史上最全單鏈表的增刪改查反轉等操作彙總以及5種排序算法(C語言)

工科生一枚,熱衷於底層技術開發,有強烈的好奇心,感興趣內容包括單片機,嵌入式Linux,Uboot等,歡迎學習交流!
愛好跑步,打籃球,睡覺。
歡迎加我QQ1500836631(備註CSDN),一起學習交流問題,分享各種學習資料,電子書籍,學習視頻等。

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 = 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 = 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 = 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 == 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 == 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種排序算法介紹。

以上代碼均爲測試後的代碼。如有錯誤和不妥的地方,歡迎指出。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章