目錄
- 1.準備作業
- 2.創建鏈表
- 3.列印鏈表
- 4.在元素后面插入元素
- 5.在元素前面增加元素
- 6.洗掉鏈表元素,要注意洗掉鏈表尾還是鏈表頭
- 7.根據傳入的數值查詢鏈表
- 8.修改鏈表元素
- 9.求鏈表長度
- 10.前驅,后繼節點的查找
- 11.倒置鏈表
- 12.判斷鏈表是否有環
1.準備作業
首先包含頭檔案,定義鏈表結構體,產生隨即鏈表的范圍,定義全域頭尾節點,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10
/*定義鏈表*/
typedef struct Node
{
int data;
struct Node *next;
}Node;
/*定義全域頭尾節點*/
Node *head = NULL;
Node *end = NULL;
2.創建鏈表
/*根據傳入的引數添加鏈表節點*/
int CreatList(int a)
{
/*定義臨時結構體并分配空間*/
Node *temp = (Node *)malloc(sizeof(Node));
if (temp ==NULL)
{
printf("malloc error!");
return -1;
}
else
{
/*給資料型別賦值*/
temp->data = https://www.cnblogs.com/dongxb/p/a;
temp->next = NULL;
/*如果鏈表長度為0*/
if (head == NULL)
{
head = temp;
end = temp;
}
else
{
end->next = temp;
end = temp;
}
}
}
3.列印鏈表
/*列印鏈表*/
void PrintList(Node *temp)
{
if(temp == NULL)
{
printf("Empty List!\r\n");
}
while (temp)
{
printf("%d",temp->data);
temp = temp->next;
if(temp)
printf("->");
}
printf("\r\n");
}
4.在元素后面插入元素
向鏈表中增添元素,根據添加位置不同,可分為以下 3 種情況:
1.插入到鏈表的頭部(頭節點之后),作為首元節點;
2.插入到鏈表中間的某個位置;
3.插入到鏈表的最末端,作為鏈表中最后一個資料元素;
雖然新元素的插入位置不固定,但是鏈表插入元素的思想是固定的,只需做以下兩步操作,即可將新元素插入到指定的位置:
a.將新結點的 next 指標指向插入位置后的結點;
b.將插入位置前結點的 next 指標指向插入結點;
例如,我們在鏈表 {1,2,3,4} 的基礎上分別實作在頭部、中間部位、尾部插入新元素 5,其實作程序如圖 所示:

/*根據傳入的數,在其后面增加元素*/
int InsertListEnd(int index,int a)
{
if (head == NULL)
{
printf("Empty List!\r\n");
return 0;
}
if (FindList(index)->next == FindList(a))
return 0;
else
{
/*找到傳入值的位置并保存*/
Node *temp = FindList(index);
/*分配空間存放新的傳入的值*/
Node *pt = (Node *)malloc(sizeof(Node));
pt->data = https://www.cnblogs.com/dongxb/p/a;
/*是否是最后一個元素*/
if (temp == end)
{
//尾巴的下一個指向新插入的節點
end->next = temp;
//新的尾巴
end = temp;
}
else
{
// 先連后面 (先將要插入的節點指標指向原來找到節點的下一個)
pt->next = temp->next;
//后連前面
temp->next = pt;
printf("The list after insert %d is \r\n",a);
PrintList(head);
}
}
}
5.在元素前面增加元素
/*根據傳入的數,在其前面增加元素*/
int InsertListHead(int index,int a)
{
if (head == NULL)
{
printf("Empty List!\r\n");
return 0;
}
/*要插入的位置就在原位*/
if (FindList(index)->next == FindList(a))
return 0;
else
{
/*找到傳入值的位置并保存*/
Node *temp = FindList(index);
/*分配空間存放新的傳入的值*/
Node *pt = (Node *)malloc(sizeof(Node));
pt->data = https://www.cnblogs.com/dongxb/p/a;
/*是否是第一個元素*/
if (temp == head)
{
//尾巴的下一個指向新插入的節點
pt->next = head;
//新的頭
head = pt;
}
else
{
/*尋找到要插入位置的前驅節點*/
Node *pre = FindPreNode(temp);
pre->next = pt;
pt->next = temp;
printf("The list after insert %d is \r\n",a);
PrintList(head);
}
}
}
6.洗掉鏈表元素,要注意洗掉鏈表尾還是鏈表頭
從鏈表中洗掉指定資料元素時,實則就是將存有該資料元素的節點從鏈表中摘除,但作為一名合格的程式員,要對存盤空間負責,對不再利用的存盤空間要及時釋放,因此,從鏈表中洗掉資料元素需要進行以下 2 步操作:
1.將結點從鏈表中摘下來;
2.手動釋放掉結點,回收被結點占用的存盤空間;
其中,從鏈表上摘除某節點的實作非常簡單,只需找到該節點的直接前驅節點 temp,執行一行程式:
temp->next=temp->next->next;
例如,從存有 {1,2,3,4} 的鏈表中洗掉元素 3,則此代碼的執行效果如圖 2 所示:

/*洗掉鏈表頭*/
void DeleteListHead()
{ //記住舊頭
Node *temp = head;
//鏈表檢測
if (NULL == head)
{
printf("Empty list!\n");
return;
}
head = head->next; //頭的第二個節點變成新的頭
free(temp);
}
/*尾洗掉————刪*/
void DeleteListTail()
{
if (NULL == end)
{
printf("鏈表為空,無需洗掉\n");
return;
}
//鏈表不為空
//鏈表有一個節點
if (head == end)
{
free(head);
head = NULL;
end = NULL;
}
else
{
//找到尾巴前一個節點
Node *temp = head;
while (temp->next != end)
{
temp = temp->next;
}
//找到了,刪尾巴
//釋放尾巴
free(end);
//尾巴遷移
end = temp;
//尾巴指標為NULL
end->next = NULL;
}
}
/*洗掉鏈表任意元素*/
void DeleteList(int a)
{
//鏈表判斷 是不是沒有東西
if (NULL == head)
{
printf("Empty list!\n");
return;
}
//鏈表有東西,找這個節點
Node *temp = FindList(a);
if (NULL == temp)
{
printf("%d not find\r\n",a);
return;
}
//找到了,且只有一個節點
if (head == end)
{
free(head);
head = NULL;
end = NULL;
printf("The list after delete %d is empty!\r\n",a);
}
else if (head->next == end) //有兩個節點
{
//看是洗掉頭還是洗掉尾
if (end == temp)
{
DeleteListTail();
printf("The list after delete %d is \r\n",a);
PrintList(head);
}
else if (temp == head)
{
DeleteListHead();
printf("The list after delete %d is \r\n",a);
PrintList(head);
}
}
else //多個節點
{
//看是洗掉頭還是洗掉尾
if (end == temp)
DeleteListTail();
else if (temp == head)
DeleteListHead();
else //洗掉中間某個節點
{ //找要洗掉temp前一個,遍歷
Node *pt = head;
while (pt->next != temp)
{
pt = pt->next;
}
//找到了
//讓前一個直接連接后一個 跳過指定的即可
pt->next = temp->next;
free(temp);
printf("The list after delete %d is \r\n",a);
PrintList(head);
}
}
}
7.根據傳入的數值查詢鏈表
/*根據傳入的數值,查詢鏈表*/
Node *FindList(int a)
{
Node *temp = head;
if(head == NULL)
{
printf("Empty List!\r\n");
return NULL;
}
else
{
while (temp)
{
if (temp->data =https://www.cnblogs.com/dongxb/p/= a)
{
printf("%d find!\r\n",a);
return temp;
}
temp = temp->next;
}
printf("%d not find!\r\n",a);
return 0;
}
}
8.修改鏈表元素
/*修改鏈表元素,element為要修改的元素,modify為修改后的值*/
void ModifyList(Node *phead,int element,int modify)
{
Node *temp = phead;
while((temp!= NULL))
{
if(temp->data =https://www.cnblogs.com/dongxb/p/= element)
{
temp->data = modify;
}
temp = temp->next;
}
}
9.求鏈表長度
/*求鏈表長度并回傳*/
int LengthList(Node *temp)
{
int length = 0;
while (temp)
{
length++;
temp = temp->next;
}
return length;
}
10.前驅,后繼節點的查找
Node *FindPreNode(Node *p)
{
Node *temp = head;
/*尋找p的前驅節點*/
if(p == head)
{
printf("%d is head node\r\n",p->data);
return NULL;
}
else
{
while((temp->next != p) && (temp !=NULL))
{
temp = temp->next;
}
return temp;
}
}
Node *FindNextNode(Node *p)
{
Node *temp = head;
/*尋找p的后繼節點*/
while(temp &&(temp != p))
{
temp = temp->next;
}
/*先不判斷是否為尾節點,尾節點NULL也可以賦值*/
temp = temp->next;
return temp;
}
11.倒置鏈表
/*方法一:倒置鏈表*/
Node *InvertList(Node *phead)
{
if(phead == NULL || phead->next == NULL)
{
return phead;
}
else
{
Node *p = phead;
Node *q = NULL;
Node *r = NULL;
while(p != NULL)
{
/*保存下一個節點*/
q = p->next;
/*讓該節點指向上一個節點*/
p->next = r;
/*上一個節點走到當前節點*/
r = p;
/*當前節點走到下一個節點*/
p = q;
}
head = r;
return head;
}
}
/*方法二:倒置鏈表*/
Node *ReverseList(Node *phead)
{
/*創建一個新鏈*/
/*兩個指標,一個指向新的鏈表,一個指向單個斷開的節點元素,連接起來*/
Node *ptmp = NULL;
Node *tmp = NULL;
/*處理鏈表為空*/
if(NULL == phead)
{
printf("link is empty\n");
return NULL;
}else
{
/*將舊鏈上的結點鏈到新鏈上*/
while(phead != NULL)
{
tmp = phead;
phead = phead->next;
/*連接到上一次存下來的連表上,第一次時,ptmp為空,整個鏈表賦值給tmp后只剩下第一個元素*/
tmp->next = ptmp;
/*新的鏈表賦值給ptmp*/
ptmp = tmp;
}
}
head = ptmp;
return ptmp;
}
12.判斷鏈表是否有環
/*判斷鏈表有環*/
int Is_Circular(Node *phead)
{
if(phead == NULL || phead->next == NULL){
return 0;
}
/*快慢指標,當二者相等時,一定有環*/
Node *p1 = phead;
Node *p2 = phead;
while(p1 != NULL && p2 != NULL){
p2 = p2->next;
if(p1 == p2)
return 1;
p2 = p2->next;
p1 = p1->next;
}
return 0;
}
測驗函式
int main ()
{
int i = 0;
/*設定獲得亂數的種子(固定代碼,沒有這句,亂數是固定不變的)測驗可以不加*/
srand((int)time(0));
for (i =5;i>0;i--)
CreatList(rand()%MAX);
// CreatList(i);
printf("新創建的的鏈表為:");
PrintList(head);
InsertListHead(4,10);
printf("在4前插入10后的鏈表為:");
PrintList(head);
InsertListEnd(4,10);
printf("在4后插入10后的鏈表為:");
PrintList(head);
DeleteList(0);
printf("洗掉0后的鏈表為:");
PrintList(head);
Node *p = FindList(7);
Node *q = FindList(4);
ModifyList(head,1,15);
printf("修改1為15后的鏈表為:");
PrintList(head);
ReverseList(head);
printf("反轉后的鏈表為:");
PrintList(head);
printf("鏈表長度為:%d",LengthList(head));
return 0;
}
測驗截圖

關于排序演算法的講解將在下節[單鏈表的5種排序演算法]介紹,
以上代碼均為測驗后的代碼,如有錯誤和不妥的地方,歡迎指出,
如遇到排版錯亂的問題,可以通過以下鏈接訪問我的CSDN,
CSDN:CSDN搜索“嵌入式與Linux那些事”
歡迎歡迎關注我的公眾號:嵌入式與Linux那些事,領取秋招筆試面試大禮包(華為小米等大廠面經,嵌入式知識點總結,筆試題目,簡歷模版等)和2000G學習資料,

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/235608.html
標籤:嵌入式
上一篇:【centos-硬碟永久掛載】
下一篇:痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU硬體那些事(2.5)- 串行NOR Flash下載演算法(IAR EWARM篇)
