日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

双向链表简单实现及图示

發布時間:2024/4/18 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 双向链表简单实现及图示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

雙向鏈表

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

和單向鏈表相比有以下優勢:

插入刪除不需要移動元素外,可以原地插入刪除

可以雙向遍歷

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

初始化+尾插法圖示://head始終指向頭結點,p指向尾節點,方便后續算法使用

刪除單個圖示:

實現代碼:

?

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 typedef struct Node pNode; 5 struct Node 6 { 7 int data; 8 pNode *prev, *next; 9 }; 10 /* 初始化鏈表,尾插法 */ 11 pNode *InitList(pNode **head, int n) 12 { 13 pNode *p, *s; 14 (*head) = (pNode *)malloc(sizeof(pNode)); 15 if ((*head) == NULL) 16 exit(0); 17 (*head)->next = NULL;//head的prev和next均指向NULL 18 (*head)->prev = NULL; 19 p = (*head);//p指向head 20 int i; 21 for (i = 0; i < n; ++i) 22 { 23 s = (pNode *)malloc(sizeof(pNode)); 24 if (s == NULL) 25 exit(0); 26 printf("Input the value of the %dth node:", i + 1); 27 scanf("%d", &s->data); 28 s->next = NULL; 29 p->next = s; 30 s->prev = p; 31 p = s;//p指向尾節點 32 } 33 return p; 34 } 35 /* 遍歷打印 */ 36 void PrintList(pNode *head) 37 { 38 pNode *p; 39 p = head->next; 40 if (head->next == NULL) 41 printf("the list is empty\n"); 42 while(p != NULL) 43 { 44 printf("%d ", p->data); 45 p = p->next; 46 } 47 printf("\n"); 48 } 49 /* 清空鏈表 */ 50 void DeleteList(pNode **head) 51 { 52 pNode *p; 53 while((*head)->next != NULL) 54 { 55 p = (*head); 56 p->next->prev = NULL; 57 (*head) = p->next; 58 free(p); 59 } 60 } 61 /* 查找鏈表內的某個值 */ 62 int SearchList(pNode *head) 63 { 64 int number; 65 printf("Values are about to be deleted:"); 66 scanf("%d", &number); 67 pNode *p; 68 p = head->next; 69 while(p != NULL) 70 { 71 if (p->data == number) 72 { 73 return number; 74 } 75 p = p->next; 76 } 77 return 0; 78 } 79 /* 刪除鏈表中某個元素,令p的前驅節點和后驅節點相互指向即可,如果p是尾節點則直接將前驅節點指向NULL*/ 80 void DelNumqList(pNode **head, int n) 81 { 82 int i; 83 pNode *p; 84 p = (*head)->next; 85 for (i = 1; i < n; ++i) 86 p = p->next; 87 if(p->next == NULL) 88 { 89 p->prev->next = NULL; 90 free(p); 91 } 92 else 93 { 94 p->next->prev = p->prev; 95 p->prev->next = p->next; 96 free(p); 97 } 98 } 99 int main(int argc, char const *argv[]) 100 { 101 int n, element, flag; 102 pNode *head, *last; 103 /***************************************************************/ 104 printf("Please input the size of the list:"); 105 scanf("%d", &n); 106 last = InitList(&head, n);//初始化鏈表并賦值,返回尾節點last 107 printf("%d %d \n", head->next->data, last->data); //打印為第一個元素和最后一個元素 108 PrintList(head); 109 /***************************************************************/ 110 flag = SearchList(head); //搜索某個值并刪除節點 111 if (flag > 0 && flag <= n) 112 { 113 DelNumqList(&head, flag); 114 PrintList(head); 115 } 116 else 117 printf("Element does not exist, cannot be deleted\n"); 118 /***************************************************************/ 119 DeleteList(&head);//清空列表 120 PrintList(head); 121 return 0; 122 }

總結

以上是生活随笔為你收集整理的双向链表简单实现及图示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。