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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

再写双向循环链表

發布時間:2023/11/30 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 再写双向循环链表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#pragma once #include<assert.h> #include<malloc.h> #include<stdio.h> typedef int DLDataType;//定義鏈表結點結構 typedef struct DListNode{DLDataType value;struct DListNode *prev; //指向前一個結點struct DListNode *next; //指向后一個結點 } DListNode;//定義雙向鏈表結構 typedef struct DList{DListNode *head; //表示鏈表頭結點 }DList;DListNode *DListBuyNode(DLDataType value){DListNode *node = (DListNode *)malloc(sizeof (DListNode));node->value = value;node->next =node->prev= NULL;return node; }//初始化 void DListInit(DList *dlist){dlist->head = DListBuyNode(0);dlist->head->next = dlist->head;dlist->head->prev = dlist->head; }//銷毀 //1. 清空鏈表 void DListClear(DList *dlist){DListNode *cur, *next;cur = dlist->head->next;while (cur != dlist->head){next = cur->next;free(cur);cur = next;}dlist->head->next = dlist->head->prev = dlist->head; }//2. 徹底銷毀鏈表 void DListDestroy(DList *dlist){DListClear(dlist);free(dlist->head);dlist->head = NULL; }//頭插 void DListPushFront(DList *dlist, DLDataType value){DListNode *node = DListBuyNode(value);node->prev = dlist->head;node->next = dlist->head->next;dlist->head->next->prev = node;dlist->head->next = node;}//尾插 void DlistPushBack(DList *dlist, DLDataType value){DListNode *node = DListBuyNode(value);node->prev = dlist->head->prev;node->next = dlist->head;node->prev->next = node;node->next->prev = node; }//頭刪 void DlistPopFront(DList *dlist){assert(dlist->head->next != dlist->head);DListNode *cur = dlist->head->next; //cur指向第一個結點dlist->head->next = cur->next; //頭結點指向第二個結點cur->next->prev = dlist->head; //第二個結點前指針指向頭結點free(cur); //刪除第一個結點 }//尾刪 void DlistPopBack(DList *dlist){assert(dlist->head->next != dlist->head); //確保鏈表不為空DListNode *cur = dlist->head->prev;cur->prev->next = dlist->head;cur->next->prev = cur->prev;free(cur); }//查找 DListNode * DListFind(const DList *dlist, DLDataType value){DListNode *cur;for (cur = dlist->head->next; cur != dlist->head; cur = cur->next){if (cur->value == value){return cur;}}return NULL; }void DListInsert(DListNode *pos, DLDataType value){DListNode *node = DListBuyNode(value);node->prev = pos->prev;node->next = pos;node->prev->next = node;pos->prev = node; }void DListErase(DListNode *pos){pos->prev->next = pos->next;pos->next->prev = pos->prev;free(pos); }void DListPrintf(const DList *dlist){for (DListNode *cur = dlist->head->next; cur != dlist->head; cur = cur->next){printf("%d-->", cur->value);}printf("\n"); }void TestDList(){DList list;DListInit(&list);DListPrintf(&list);DlistPushBack(&list, 1);DlistPushBack(&list, 2);DlistPushBack(&list, 3);DListPrintf(&list);DListPushFront(&list, 11);DListPushFront(&list, 12);DListPushFront(&list, 13);DListPrintf(&list); } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的再写双向循环链表的全部內容,希望文章能夠幫你解決所遇到的問題。

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