1、双向循环链表
双向链表是在单链表的每个结点中,再设置一个纸箱其前驱结点的指针域。双向链表可以有效的提高算法的时间性能,用空间换取时间。
typedef struct Node { ElemType data; struct Node* prior; //直接前驱指针 struct Node* next; //直接后继指针 }Node, * CLinkList;
2、双向链表的插入操作:(注意顺序)
s->next=p; s->prior=p->prior; p->prior->next=s; p->prior=s;
删除结点:
p->prior->next=p->next; p->next->prior=p->prior; free(p);
3、例子:
#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; }
原文地址:https://www.cnblogs.com/lemonzhang/p/12336864.html
时间: 2024-10-08 10:24:53