双向链表(带头结点)
生活随笔
收集整理的這篇文章主要介紹了
双向链表(带头结点)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
帶頭結點雙向鏈表的內存分布情況
頭文件
#pragma once //雙向鏈表 typedef struct DNode {int data;DNode* next;DNode* prio; }DNode , *DList ;//初始化 void InitList(DList plist);//頭插法 bool Insert_head(DList plist,int val);//尾插法 bool Insert_tail(DList plist,int val);//在pos下標插入數據val bool Insert_pos(DList plist,int pos,int val);//查找,找到返回節點地址,沒有找到返回NULL DNode *Search(DList plist,int key);//刪除第一個key對應的節點 bool Delete(DList plist,int key);//刪除第一個數據節點,并通過rtval獲得刪除的值 bool Delete_head(DList plist,int *rtval);//刪除最后一個數據節點,并通過rtval獲得刪除的值 bool Delete_tail(DList plist,int *rtval);//獲取長度,統計數據節點的個數 int GetLength(DList plist);//判空 bool IsEmpty(DList plist);//清除所以數據 void Clear(DList plist);//銷毀所有節點 void Destroy(DList plist);//打印 void Show(DList plist);//反轉 void Reverse(DList plist);cpp文件
#include<stdlib.h> #include<assert.h> #include"dlist.h" #include<stdio.h> //初始化 void InitList(DList plist) {assert(plist != NULL);if(plist == NULL){return ;}plist->next = NULL;plist->prio = NULL; } static DNode *BuyNode(int val) {DNode* p = (DNode*) malloc (sizeof(DNode));p->data = val;return p; }//頭插法 bool Insert_head(DList plist,int val) {DNode * p = BuyNode(val);assert(plist != NULL);if(plist == NULL){return false;}if(plist->next != NULL){plist->next->prio = p;}p->prio = plist;p->next = plist ->next;plist->next = p;return true; }//尾插法 bool Insert_tail(DList plist,int val) {DNode * p = BuyNode(val);assert(plist != NULL);if(plist == NULL){return false;}DNode *q;for(q = plist;q->next!= NULL;q = q->next);//將p 插在q 的后面q->next =p;p->prio =q;p->next = NULL;return true; }//在pos下標插入數據val bool Insert_pos(DList plist,int pos,int val) {DNode * p = BuyNode(val);assert(plist != NULL);if(plist == NULL){return false;}DNode *q;int i = 0;for( q =plist->next; q!= NULL && i<pos;q = q->next);if(i < 0){return false;}p->next = q->next;p->prio = q;q->next = p;return true; }//查找,找到返回節點地址,沒有找到返回NULL DNode *Search(DList plist,int key) {assert(plist != NULL);if(plist == NULL){return false;}for(DNode *p = plist->next;p != NULL;p = p->next){if(p->data == key){return p;}}return NULL;}//刪除第一個key對應的節點 bool Delete(DList plist,int key) {assert(plist != NULL);if(plist == NULL){return false;}DNode * p =Search(plist,key);if(p == NULL){return false;}p->prio->next = p->next;if(p->next != NULL){p->next->prio = p->prio;}free(p);return true;}//刪除第一個數據節點,并通過rtval獲得刪除的值 bool Delete_head(DList plist,int *rtval) {assert(plist != NULL);if(plist != NULL || plist->next == NULL){return false;}if(rtval != NULL){*rtval = plist->data;}DNode *p = plist->next;plist->next = p->next;p->next ->prio = plist;free(p);return true; }//刪除最后一個數據節點,并通過rtval獲得刪除的值 bool Delete_tail(DList plist,int *rtval) {assert(plist != NULL);if(plist == NULL || plist->next == NULL){return false;}DNode *p = plist ;DNode *q;for(q = plist; q->next != NULL;q = q->next);q->next = p;if(rtval != NULL){*rtval = p->data;}q->next = NULL;free(p);return true; }//獲取長度,統計數據節點的個數 int GetLength(DList plist) {int length = 0;for(DNode *p=plist->next;p!=NULL;p=p->next){length++;}return length; }//判空 bool IsEmpty(DList plist) {return plist->next == NULL; }//清除所有數據 void Clear(DList plist) {Destroy(plist); }//銷毀所有節點 void Destroy(DList plist) {DNode *p;while(plist->next != NULL){p = plist->next;plist->next = p->next;free(p);} }//打印 void Show(DList plist) {for(DNode *p=plist->next;p!=NULL;p=p->next){printf("%d ",p->data);}printf("\n"); }//反轉 void Reverse(DList plist) {DNode * q;DNode * p = plist->next;while(p != NULL){q = p;p = p->next;q->next = plist ->next;plist ->next = p;}}主函數
#include<stdio.h> #include<stdlib.h> #include"dlist.h" int main() {DNode dlist;InitList(&dlist);for(int i=0;i<10;i++){Insert_head(&dlist,i);}Show(&dlist);Reverse(&dlist);Show(&dlist);return 0; }?
總結
以上是生活随笔為你收集整理的双向链表(带头结点)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为手机怎么看图片属性_华为手机怎么才能
- 下一篇: BugkuCTF-PWN题pwn1-瑞士