内核里用的最多就是双向循环链表,今天我回顾了双向链表的知识,写了一个创建链表的程序,拿出来分享一下:
- #include
- #include
- #include
- typedef struct DNode {
- int data;
- struct DNode *prev,*next;
- }DNode,*DoubleList;
- void print(DoubleList head);
- int main()
- {
- DoubleList next,prev,head;
- prev=head=(DNode *)malloc(sizeof(DNode));
- next=(DNode *)malloc(sizeof(DNode));
- scanf("%d",&next->data);
- while(next->data!=0)
- {
- prev->next=next;
- next->prev=prev;
- prev=next;
- next=(DNode *)malloc(sizeof(DNode));
- scanf("%d",&next->data);
- }
- prev->next=head;
- head->prev=prev;
- free(next);
- print(head);
- return 0;
- }
- void print (DoubleList head)
- {
- DoubleList L;
- L=head->next;
- while(L!=head)
- {
- printf("%d ",L->data);
- L=L->next;
- }
- printf("\n");
- }
阅读(4974) | 评论(2) | 转发(0) |