生活随笔
收集整理的這篇文章主要介紹了
单向链表的C语言实现与基本操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文的主要內容目錄:
一、單向鏈表的C語言實現
二、單向鏈表的基本操作
一、單向鏈表的C語言實現
鏈表作為一種基本的數據結構在程序開發過程當中經常會使用到。對C語言來說鏈表的實現主要依靠結構體和指針,所以本文相關內容和程序需要有C語言當中指針和結構體的基礎。
鏈表是一種線性存儲數據的結構,存儲內容在邏輯上連續的,在物理上卻不一定連續。單向鏈表的組成包括一個鏈表頭(head)和若干鏈表元素(node),對鏈表的基本操作其實就是增、刪、改、查。
首先說說單向鏈表的C語言實現方法。為了實現一個單向鏈表,首先定義一個結構體:
[cpp] view plain
copy ??struct?list?{??????int?id;???????????????char?data[20];????????????struct?list?*next;????};??
下面來編寫程序實現一個鏈表的基本操作。該程序的功能是首先分配若干個鏈表元素(node),然后對這些鏈表元素進行賦初值,賦完初值之后將這些鏈表元素依次加入到鏈表當中,最后把這些元素的data字段依次打印出來。(注意 :本程序實現的鏈表頭結點(head)當中是存放數據的;為了保證每一個元素的id字段都不相同,所以定義了一個全局靜態數據成員list_id;使用linux下的gcc進行編譯)。程序如下:
[cpp] view plain
copy ??#include?<stdio.h>??#include?<stdlib.h>??????struct?list?{??????int?id;???????????????char?data[20];????????????struct?list?*next;????};??????static?struct?list?*list_head?=?NULL;??????static?int?list_id?=?0;?????????static?void?list_add(struct?list?**head,?struct?list?*list)??{??????struct?list?*temp;??????????????if(NULL?==?*head)??????{????????????????????*head?=?list;??????????(*head)->next?=?NULL;??????}??????else??????{????????????????????temp?=?*head;??????????while(temp)??????????{??????????????if(NULL?==?temp->next)??????????????{??????????????????temp->next?=?list;??????????????????list->next?=?NULL;??????????????}??????????????temp?=?temp->next;??????????}??????}??}????????static?void?list_print(struct?list?**head)??{?????????struct?list?*temp;????????temp?=?*head;????????printf("list?information?:\n");??????while(temp)??????{??????????printf("\tlist?%d?:?%s\n",?temp->id,?temp->data);??????????temp?=?temp->next;??????}??}??????int?main(int?argc,?char?*argv[])??{??????int?i?=?0;??????struct?list?*lists?=?NULL;??????????????lists?=?malloc(sizeof(struct?list)?*?10);??????if(NULL?==?lists)??????{??????????printf("malloc?error!\n");??????????return?-1;??????}??????????????for(i?=?0;?i?<?10;?i++)??????{??????????lists[i].id?=?list_id++;??????????sprintf(lists[i].data,?"TECH-PRO?-?%d",?i);????????????list_add(&list_head,?&lists[i]);??????}??????????????list_print(&list_head);????????return?0;??}??
程序的運行結果如下所示:
二、單向鏈表的基本操作
鏈表的基本操作其實就是對鏈表中元素的操作。鏈表的基本操作包括鏈表中結點元素的添加、刪除、修改和查看,簡單來說就是增、刪、改、查。
2.1 單向鏈表中元素添加
在鏈表中添加元素時首先判斷鏈表是否為空,為空時直接把要添加的元素賦值給鏈表頭部,否者就要對鏈表進行遍歷找到下一個為空的結點,然后把元素插入到鏈表尾部。
實現鏈表插入的程序代碼:
[cpp] view plain
copy ?????static?void?list_add(struct?list?**head,?struct?list?*list)??{??????struct?list?*temp;??????????????if(NULL?==?*head)??????{????????????????????*head?=?list;??????????(*head)->next?=?NULL;??????}??????else??????{????????????????????temp?=?*head;??????????while(temp)??????????{??????????????if(NULL?==?temp->next)??????????????{??????????????????temp->next?=?list;??????????????????list->next?=?NULL;??????????????}??????????????temp?=?temp->next;??????????}??????}??}??
在main函數中田間如下代碼:
[cpp] view plain
copy struct?list?temp_list;??????????temp_list.id?=?list_id++;??????sprintf(temp_list.data,?"temp_list");??????list_add(&list_head,?&temp_list);??
對程序進行編譯,結果如下:
2.2 在單向鏈表中刪除元素
在鏈表刪除一個元素結點是鏈表基本操作中相對復雜的。首先需要判斷鏈表是否為空,當鏈表不為空時,要再次判斷要刪除的元素是否是頭結點(head),如果是直接將下一個元素賦值給頭結點,不過不是就需要遍歷整個鏈表找到要刪除的結點元素。(注:本算法實現的刪除是通過對元素結點的id字段進行的)。
刪除鏈表結點的程序代碼:
[cpp] view plain
copy ??????static?int?list_del(struct?list?**head,?int?id)??{??????struct?list?*temp,?*p;??????temp?=?*head;????????if(NULL?==?temp)??????{????????????????????printf("鏈表為空!\n");??????????return?-1;??????}??????else??????{????????????????????if(id?==?temp->id)?????????????????{??????????????*head?=?temp->next;??????????????return?0;??????????}??????????else??????????????????????????????{??????????????while(temp->next)??????????????{??????????????????p?=?temp;??????????????????temp?=?temp->next;????????????????????if(id?==?temp->id)??????????????????{??????????????????????p->next?=?temp->next;??????????????????????return?0;??????????????????}??????????????}?????????????????return?-1;??????????}??????}????????return?-1;??}??
在main函數中添加如下代碼:
[cpp] view plain
copy ??????list_del(&list_head,?0);??????list_del(&list_head,?5);??????list_del(&list_head,?10);??
對程序進行編譯結果如下所示,第0、5、10號元素都被刪除:
2.3 在單向鏈表中修改指定的元素
在鏈表中修改元素,通過對鏈表中元素進行遍歷,找到要修改的元素對其內容分進行修改即可,實現鏈表元素的修改是相對容易的。(注 :對要修改的元素的定位是通過id字段來完成的)
鏈表中修改元素的程序如下:
[cpp] view plain
copy ???????static?int?list_chg(struct?list?**head,?int?id,?char?*content)??{??????struct?list?*temp;????????temp?=?*head;???????????while(temp)???????????{??????????if(id?==?temp->id)??????????{??????????????memset(temp->data,?0,?sizeof(temp->data));??????????????sprintf(temp->data,?"%s",?content);??????????????temp->data[strlen(content)]?=?'\0';??????????????return?0;??????????}??????????temp?=?temp->next;??????}??????return?-1;??}??
在main函數中添加如下代碼:
[cpp] view plain
copy ??????list_chg(&list_head,?4,?"change!!!");??
編譯程序,運行結果如下所示:
2.4 對單向鏈表的元素進行查詢操作
在鏈表中查詢一個元素,根據鏈表頭部對鏈表進行遍歷即可。
鏈表中查詢的程序代碼:
[cpp] view plain
copy ??????static?int?list_query(struct?list?**head,?int?id)??{??????struct?list?*temp;????????temp?=?*head;???????????while(temp)???????????{??????????if(id?==?temp->id)??????????{??????????????printf("list?%d?:?%s\n",?temp->id,?temp->data);??????????????return?0;??????????}??????????temp?=?temp->next;??????}??????????????printf("not?finding!\n");????????????return?-1;??}??
程序運行結果:
附錄:單向鏈表的基本操作已經說完,附上一個完整的程序。
[cpp] view plain
copy ??#include?<stdio.h>??#include?<stdlib.h>??#include?<string.h>??????struct?list?{??????int?id;???????????????????char?data[20];????????????struct?list?*next;????};??????static?struct?list?*list_head?=?NULL;??????static?int?list_id?=?0;?????????static?void?list_add(struct?list?**head,?struct?list?*list)??{??????struct?list?*temp;??????????????if(NULL?==?*head)??????{????????????????????*head?=?list;??????????(*head)->next?=?NULL;??????}??????else??????{????????????????????temp?=?*head;??????????while(temp)??????????{??????????????if(NULL?==?temp->next)??????????????{??????????????????temp->next?=?list;??????????????????list->next?=?NULL;??????????????}??????????????temp?=?temp->next;??????????}??????}??}????????static?void?list_print(struct?list?**head)??{?????????struct?list?*temp;????????temp?=?*head;????????printf("list?information?:\n");??????while(temp)??????{??????????printf("\tlist?%d?:?%s\n",?temp->id,?temp->data);??????????temp?=?temp->next;??????}??}??????????static?int?list_del(struct?list?**head,?int?id)??{??????struct?list?*temp,?*p;??????temp?=?*head;????????if(NULL?==?temp)??????{????????????????????printf("鏈表為空!\n");??????????return?-1;??????}??????else??????{????????????????????if(id?==?temp->id)?????????????????{??????????????*head?=?temp->next;??????????????return?0;??????????}??????????else??????????????????????????????{??????????????while(temp->next)??????????????{??????????????????p?=?temp;??????????????????temp?=?temp->next;????????????????????if(id?==?temp->id)??????????????????{??????????????????????p->next?=?temp->next;??????????????????????return?0;??????????????????}??????????????}?????????????????return?-1;??????????}??????}????????return?-1;??}???????????static?int?list_chg(struct?list?**head,?int?id,?char?*content)??{??????struct?list?*temp;????????temp?=?*head;???????????while(temp)???????????{??????????if(id?==?temp->id)??????????{??????????????memset(temp->data,?0,?sizeof(temp->data));??????????????sprintf(temp->data,?"%s",?content);??????????????temp->data[strlen(content)]?=?'\0';??????????????return?0;??????????}??????????temp?=?temp->next;??????}??????return?-1;??}??????????static?int?list_query(struct?list?**head,?int?id)??{??????struct?list?*temp;????????temp?=?*head;???????????while(temp)???????????{??????????if(id?==?temp->id)??????????{??????????????printf("list?%d?:?%s\n",?temp->id,?temp->data);??????????????return?0;??????????}??????????temp?=?temp->next;??????}??????????????printf("not?finding!\n");????????????return?-1;??}????????int?main(int?argc,?char?*argv[])??{??????int?i?=?0;??????struct?list?*lists?=?NULL;????????struct?list?temp_list;??????????????lists?=?malloc(sizeof(struct?list)?*?10);??????if(NULL?==?lists)??????{??????????printf("malloc?error!\n");??????????return?-1;??????}??????????????for(i?=?0;?i?<?10;?i++)??????{??????????lists[i].id?=?list_id++;??????????sprintf(lists[i].data,?"TECH-PRO?-?%d",?i);????????????list_add(&list_head,?&lists[i]);??????}??????????????temp_list.id?=?list_id++;??????sprintf(temp_list.data,?"temp_list");??????list_add(&list_head,?&temp_list);??????????????list_del(&list_head,?0);??????list_del(&list_head,?5);??????list_del(&list_head,?10);??????????????list_chg(&list_head,?4,?"change!!!");??????????????list_query(&list_head,?4);????????return?0;??}?
總結
以上是生活随笔為你收集整理的单向链表的C语言实现与基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。