c语言循环单链表初始化,C语言双向循环链表实现及图示(初始化/插入链表/清空/销毁)...

————————————————————————————————————————————

双向循环链表 //遍历等执行方法与普通双向链表相同,不单独列举

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

初始化+尾插法

图示:

20180110231914543140.png

实现代码

1 /*初始化双向循环链表,head的头尾均指向本身*/

2 void InitList(pNode **head)3 {4 pNode *p;5 *head = (pNode *)malloc(sizeof(pNode));6 if ((*head) ==NULL)7 exit(0);8 (*head)->next = (*head);9 (*head)->prev = (*head);10 }11 /*插入,在链表第n个位置插入元素*/

12 pNode *InsertList(pNode **head)13 {14 pNode *p, *s;15 int i = 0;16 p = (*head)->prev; //p始终指向尾节点

17 intn;18 printf("The input to the position of the insert:");19 scanf("%d", &n);20 s = (pNode *)malloc(sizeof(pNode));21 if (s ==NULL)22 exit(0);23 printf("Input to insert element value:");24 scanf("%d", &s->data);25 if (s->data <= 0)26 returnp;27 while(i < n - 1)28 {29 i++;30 p = p->next;31 }32 s->prev =p;33 s->next = p->next;34 p->next->prev =s;35 p->next =s;36 InsertList(head);37 }

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

销毁或清空链表:

实现代码:

1 /*清空链表,保留头指针*/

2 void ClearList(pNode **head)3 {4 pNode *p;5 p = (*head)->next;6 while(p != (*head))7 {8 p = p->next;9 free(p->prev);10 }11 (*head)->next = (*head)->prev = (*head);//头节点的两个指针域指向自身

12 }13 /*彻底销毁链表,头指针置空*/

14 void DestroyList(pNode **head)15 {16 pNode *p;17 p = (*head)->next;18 while(p != (*head))19 {20 p = p->next;21 free(p->prev);22 }23 free(*head);24 (*head) =NULL;25 }

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

完整代码:

1 #include

2 #include

3 #include

4 typedef structNode pNode;5 typedef structNode6 {7 intdata;8 struct Node *prev, *next;9 } Node;10 /*初始化双向循环链表,head的头尾均指向本身*/

11 void InitList(pNode **head)12 {13 pNode *p;14 *head = (pNode *)malloc(sizeof(pNode));15 if ((*head) ==NULL)16 exit(0);17 (*head)->next = (*head);18 (*head)->prev = (*head);19 }20 /*插入,在链表第n个位置插入元素*/

21 pNode *InsertList(pNode **head)22 {23 pNode *p, *s;24 int i = 0;25 p = (*head)->prev; //p始终指向尾节点

26 intn;27 printf("The input to the position of the insert:");28 scanf("%d", &n);29 s = (pNode *)malloc(sizeof(pNode));30 if (s ==NULL)31 exit(0);32 printf("Input to insert element value:");33 scanf("%d", &s->data);34 if (s->data <= 0)35 returnp;36 while(i < n - 1)37 {38 i++;39 p = p->next;40 }41 s->prev =p;42 s->next = p->next;43 p->next->prev =s;44 p->next =s;45 InsertList(head);46 }47 /*遍历打印*/

48 void PrintList(pNode *head)49 {50 pNode *p;51 p = head->next;//从头结点之后开始循环打印

52 while(p !=head)53 {54 printf("%d", p->data);55 p = p->next;56 }57 printf("\n");58 }59 /*清空链表,保留头指针*/

60 void ClearList(pNode **head)61 {62 pNode *p;63 p = (*head)->next;64 while(p != (*head))65 {66 p = p->next;67 free(p->prev);68 }69 (*head)->next = (*head)->prev = (*head);//头节点的两个指针域指向自身

70 }71 /*彻底销毁链表,头指针置空*/

72 void DestroyList(pNode **head)73 {74 pNode *p;75 p = (*head)->next;76 while(p != (*head))77 {78 p = p->next;79 free(p->prev);80 }81 free(*head);82 (*head) =NULL;83 }84 int main(int argc, char const *argv[])85 {86 pNode *head, *last;87 InitList(&head);88 last = InsertList(&head);89 PrintList(head);90 ClearList(&head);91 printf("%p %p %p\n", head, head->next, head->prev); //验证是否头节点指向自身

92 DestroyList(&head);93 printf("%p\n",head);//验证是否已经完全销毁

94 return 0;95 }

原文:http://www.cnblogs.com/hughdong/p/6785655.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值