1, two-way circular list
Doubly linked list in a single list for each node, and then its precursor is provided a carton node pointer field. Doubly linked list can effectively improve the time performance of the algorithm, using space for time.
typedef struct the Node { elemType Data; struct the Node * Prior; // immediate predecessor pointers struct the Node * Next; // immediate successor pointers } Node, * CLinkList;
2, a doubly linked list insertion order :( Note)
s->next=p; s->prior=p->prior; p->prior->next=s; p->prior=s;
Delete node:
p->prior->next=p->next; p->next->prior=p->prior; free(p);
3, example:
#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 typedef char ElemType; typedef int Status; typedef struct DualNode { ElemType data; struct DualNode* prior; struct DualNode* next; }DualNode, *DuLinkList; Status InitList(DuLinkList *L) { DualNode *p, *q; int i; *L = (DuLinkList)malloc(sizeof(DualNode)); if (!(*L)) { return ERROR; } (*L)->prior = (*L)->next = NULL; p = (*L); for (i = 0; i < 26; i++) { q = (DualNode*)malloc(sizeof(DualNode)); if (!q) { return ERROR; } q->data = 'A' + i; q->prior = p; q->next = p->next; p->next = q; p = q; } p->next = (*L)->next; (*L)->next->prior = p; (*L)->prior = p->prior; return OK; } void Caesar(DuLinkList* L, int i) { if (i > 0) { do { (*L) = (*L)->next; } while (--i); } if (i < 0) { do { (*L) = (*L)->prior; } while (++i); } } int main() { DuLinkList L; int i,n; InitList(&L); printf("请输入一个整数:"); scanf_s("%d", &n); printf("\n"); Caesar(&L, n); for (i = 0; i < 26; i++) { L = L->next; printf("%c", (L)->data); } printf("\n"); return 0; }