标签:ast position nod ini images 微软雅黑 位置 节点 ++
————————————————————————————————————————————
双向循环链表 //遍历等执行方法与普通双向链表相同,不单独列举
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
初始化+尾插法
图示:
实现代码
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 int n; 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 return p; 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 <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 typedef struct Node pNode; 5 typedef struct Node 6 { 7 int data; 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 int n; 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 return p; 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 }
C语言双向循环链表实现及图示(初始化/插入链表/清空/销毁)
标签:ast position nod ini images 微软雅黑 位置 节点 ++
原文地址:http://www.cnblogs.com/hughdong/p/6785655.html