单链表(带头节点)
帶頭結點單鏈表的內存分布情況
?
頭文件
#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; }?
總結
- 上一篇: CTF-Crypto密码学
- 下一篇: matlab实验函数编写与程序设计,ma