创建循环双链表

来源:互联网 发布:ubuntu xorg 安装 编辑:程序博客网 时间:2024/05/21 15:26

#include <stdio.h>#include <stdlib.h>typedef int ItemValue;typedef struct DoubleLinkedList{ItemValue value;struct DoubleLinkedList *pre;struct DoubleLinkedList *next;} DoubleList, *pDoubleList;DoubleList *CreateCylicDoubleLinkedList(){printf("创建循环双链表!\n");DoubleList *head = (DoubleList *)malloc(sizeof(DoubleList));head->pre = head;head->next = head;DoubleList *pTemp;pTemp = head;ItemValue iValue;while (scanf("%d", &iValue) != EOF){DoubleList *p = (DoubleList *)malloc(sizeof(DoubleList));p->value = iValue;p->pre = pTemp;p->next = head;pTemp->next = p;pTemp = p;head->pre = p;}return head;}/* 访问循环双链表 */void VisitCylicDoubleLinkedList(DoubleList *dl){printf("循环双链表的输出!\n");DoubleList *pTemp;pTemp = dl;while (pTemp->next != dl){printf("%d ", pTemp->next->value);pTemp = pTemp->next;}}int main(){DoubleList *DL;DL = CreateCylicDoubleLinkedList();VisitCylicDoubleLinkedList(DL);free(DL);system("pause");return 0;}


测试结果:






0 0