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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

单链表(带头节点)

發布時間:2024/9/27 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单链表(带头节点) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

帶頭結點單鏈表的內存分布情況

?


頭文件

#pragma once //帶頭節點的單鏈表 //單鏈表尾節點的next為NULL //List為一條鏈表;Node* 一個節點的地址 typedef struct Node {int data;//數據Node *next;//下一個節點的地址 }Node ,*List ;//List == Node *//初始化 void InitList(List plist);//頭插法 bool Insert_head(List plist,int val);//尾插法 bool Insert_tail(List plist,int val);//在pos下標插入數據val bool Insert_pos(List plist,int pos,int val);//查找,找到返回節點地址,沒有找到返回NULL Node *Search(List plist,int key);//刪除第一個key對應的節點 bool Delete(List plist,int key);//刪除第一個數據節點,并通過rtval獲得刪除的值 bool Delete_head(List plist,int *rtval);//刪除最后一個數據節點,并通過rtval獲得刪除的值 bool Delete_tail(List plist,int *rtval);//獲取長度,統計數據節點的個數 int GetLength(List plist);//判空 bool IsEmpty(List plist);//清除所以數據 void Clear(List plist);//銷毀所有節點 void Destroy(List plist);//打印 void Show(List plist);

cpp文件

#include<stdio.h> #include<assert.h> #include<stdlib.h> #include"list.h"//初始化 void InitList(List plist) {assert(plist != NULL);plist->next = NULL; }//頭插法 bool Insert_head(List plist,int val) {assert(plist != NULL);Node *p = (Node*)malloc(sizeof(Node));p->data = val;p->next = plist->next; plist->next = p;return true; }//尾插法 bool Insert_tail(List plist,int val) {assert(plist != NULL);Node * p = (Node*)malloc(sizeof(Node));assert( p != NULL);if(p == NULL){return false;}Node *q;for(q=plist;q->next != NULL;q = q->next);//把p 插到 q 的后邊p->next = q->next;q->next = p;p->data = val;//p->next = NULL;return true;}//在pos下標插入數據val bool Insert_pos(List plist,int pos,int val) {if(pos < 0){return false;}Node *p = (Node*) malloc (sizeof(Node));Node *q = plist;int i = 0;for(;q->next!= NULL && i<pos;i++,q = q->next);if(i< pos )//判斷是否連續{return false;}p->next = q ->next;q->next = p;p->data = val; }//查找,找到返回節點地址,沒有找到返回NULL Node* Search(List plist,int key) {Node *p;for(p =plist->next;p != NULL;p=p->next){if(p->data == key ){return p ;}}//return false;return NULL; } //查找key的前趨節點 static Node *SearchPri(List plist,int key) {Node* p = (Node*) malloc (sizeof(Node));for(p = plist;p ->next != NULL; p = p->next){if(p->data == key){return p;}}return NULL; } //刪除第一個key對應的節點 bool Delete(List plist,int key) {Node *p;p = SearchPri(plist, key);//p是查找的key對應的前驅if(p == NULL){return false;}Node* q = p->next;//q 指向將要刪除的節點p->next = q->next;//將q從鏈表中剔除free(q);//釋放q }//刪除第一個數據節點,并通過rtval獲得刪除的值 bool Delete_head(List plist,int *rtval) {assert(plist != NULL);if(plist != NULL || plist->next == NULL){return false;}if(rtval != NULL){*rtval = plist->data;}Node * p = plist ->next;plist->next = p->next;free(p);}//刪除最后一個數據節點,并通過rtval獲得刪除的值 bool Delete_tail(List plist,int *rtval)//****************************** {assert(plist != NULL);if(plist == NULL || plist->next == NULL){return false;}Node *p;for(p =plist;p ->next!= NULL; p = p->next);if(rtval != NULL){*rtval = p->next->data;}p ->next = NULL;free(p);return true; }//獲取長度,統計數據節點的個數 int GetLength(List plist) {int length = 0;for(Node *p =plist->next;p != NULL ;p = p->next){length ++;}return length; }//判空 bool IsEmpty(List plist) {assert(plist != NULL);if(plist == NULL){return false;}return plist->next == NULL; }//清除所有數據 void Clear(List plist) {Destroy(plist); }//銷毀所有節點 void Destroy(List plist) {assert(plist != NULL);if(plist == NULL){return ;}while(plist ->next != NULL){Node *p = plist->next;plist ->next= p->next;free(p);} }//打印 void Show(List plist) {assert(plist != NULL);if(plist == NULL){return ;}for(Node*p = plist ->next;p != NULL;p = p->next){printf("%d ",p->data);}printf("\n"); }

主函數

#include<stdio.h> #include"list.h"int main() {Node list1;Node list2;InitList(&list1);for(int i= 0;i<10;i++){Insert_tail( &list1,i);}Show(&list1);InitList(&list2);for(int i= 0;i<10;i++){Insert_head( &list2,i);}Show(&list2);int rt = -1;printf("%d\n",Delete_tail(&list2,&rt));printf("%d\n",&rt);Destroy(&list1);Show(&list1);Destroy(&list2);Show(&list2);return 0; }

?

總結

以上是生活随笔為你收集整理的单链表(带头节点)的全部內容,希望文章能夠幫你解決所遇到的問題。

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