免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

【C·數(shù)據(jù)結(jié)構(gòu)】詳細(xì)實(shí)現(xiàn)單鏈表代碼+圖例直觀理解-創(chuàng)新互聯(lián)

文章目錄
  • 前言
  • 單鏈表的初始化
    • 1.結(jié)構(gòu)體
    • 2.初始化
  • 單鏈表節(jié)點(diǎn)的建立
    • 1.尾插
    • 2.頭插
  • 查找
  • 修改
  • 刪除
  • 添加
  • 打印
  • 代碼呈現(xiàn)
  • 總結(jié)

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),鼓樓企業(yè)網(wǎng)站建設(shè),鼓樓品牌網(wǎng)站建設(shè),網(wǎng)站定制,鼓樓網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,鼓樓網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。前言

沒(méi)前言

單鏈表的初始化 1.結(jié)構(gòu)體

鏈表的節(jié)點(diǎn)需要一個(gè)數(shù)據(jù)域和一個(gè)指針域,指針用來(lái)連接各節(jié)點(diǎn)

typedef struct Linklist
	{int key;
		struct Linklist* next;
	}List;
2.初始化

初始化需要一個(gè)頭節(jié)點(diǎn)(List* head),通過(guò)它來(lái)連接多個(gè)節(jié)點(diǎn).
先分配空間(malloc),再對(duì)相應(yīng)的參數(shù)賦值(NULL,key).(頭節(jié)點(diǎn)可以不用存儲(chǔ)數(shù)據(jù))

List* Initlist(List*head,int key)
	{head = (List*)malloc(sizeof(List));
		head->next = NULL;
		head->key = key;
		return head;
	}
單鏈表節(jié)點(diǎn)的建立

鏈表的建立首先需要建立新的節(jié)點(diǎn),用頭插和尾插兩種方式插入到鏈表中,建立的函數(shù)需要
一個(gè)數(shù)(key)和頭節(jié)點(diǎn).

List* setList(List* head, int key)
	{List* node = (List*)malloc(sizeof(List));
		node->key = key;
	}

上述這是兩種建立(或插入)的通用的方式,接下來(lái)詳細(xì)分別介紹這兩種插入方式

1.尾插

尾插是按照順序頭節(jié)點(diǎn)依次往下建立,需要一個(gè)工具指針(end)隨新節(jié)點(diǎn)建立而移動(dòng)
從而保證頭指針不動(dòng)(尾指針使用全局變量即可).下面是使用processon作圖表示鏈
接過(guò)程(用箭頭表示指針,省略數(shù)據(jù))

end->next = node;  //
	node->next = NULL;
	end = node;

在這里插入圖片描述

2.頭插

頭插和尾插不同之處在于頭插不需要移動(dòng)的指針(end),插入的節(jié)點(diǎn)從頭結(jié)點(diǎn)開(kāi)始
(node2為新節(jié)點(diǎn)node)

在這里插入圖片描述

node->next = head->next;
	head->next = node;
查找

查找需要頭節(jié)點(diǎn)(head)和所要查找的數(shù)據(jù)(key),返回類型int為該數(shù)據(jù)在鏈表的位置

int find(List* head,int key)
  {int i = 0;
  	while(head!=NULL)
  	{if(head->key)
  		{	return i;
  		}
  		head = head->next;
  		i++;
  	}
  	return -1;
  }
修改

修改操作需要一步查找操作,

List* modify(List* head,int key,int data)//key需要替換成data.
	{int index = find(head,key);
		if(index == -1)
		{	printf("Not Found");
			return;
		}
		else 
		{List* temp = head;
			for(int i = 0; i< index; i++)
			{		head = head->next;
			}//指針移動(dòng)到目標(biāo)節(jié)點(diǎn).
			head->key = data;
			return temp;	
		}
	}
刪除
  • 先找到對(duì)應(yīng)節(jié)點(diǎn)的上一個(gè)節(jié)點(diǎn)再進(jìn)行刪除操作

在這里插入圖片描述

List* delete_node(List* head,int key)
	{int index = find(head,key);
		if(index == -1)
		{	printf("Not Found");
			return;
		}
		else 
		{if(head->key == key)
			{		List* temp = head->next;
				free(head);
				return temp;
			}
			else 
			{		List* temp = head;
				while(temp->next!=NULL)//移動(dòng)到目標(biāo)節(jié)點(diǎn)前一個(gè)節(jié)點(diǎn)
				{if(temp->next->key == key)
					{break;
					}
					temp = temp->next;
				}
				List* free_node = temp->next;
				temp->next = temp->next->next;
				free(free_node);
				return head;
			}
		}
	}
添加

在某數(shù)據(jù)后添加一個(gè)新節(jié)點(diǎn)
在這里插入圖片描述

List*  insert(List* head,int key,int data)
	{int index = find(head,key);
		if(index == -1)
		{	printf("Not Found");
			return;
		}
		else 
		{	List* temp = head;
			for(int i = 0; i< index; i++)
			{		head = head->next;
			}//指針移動(dòng)到目標(biāo)節(jié)點(diǎn).
				List* node = (List*)malloc(sizeof(List));
				node->next = head->next;
				head->next = node;
				node->key = data;
				return temp;
		}
	}
打印
void print(List* head)
{while(head!=NULL)
	{printf("%d ",head->key);
		head = head->next;
	}
	printf("\n");
}
代碼呈現(xiàn)
#include#includetypedef struct Linklist
{int key;
	struct Linklist* next;
}List;
List* end;
List* Initlist(List* head, int key);
List* setList(List* head, int key);
int find(List* head, int key);
List* modify(List* head, int key, int data);
void print(List* head);
List* insert(List* head, int key, int data);
List* delete_node(List* head, int key);

int main(void)
{List* head = {NULL};
	head = Initlist(head,1);
	head = setList(head, 2);
	head = setList(head, 3);
	head = setList(head, 4);
	head = setList(head, 5);
	head = setList(head, 6);
	print(head);
	head = insert(head, 5, 99);
	print(head);
	head = delete_node(head, 5);
	print(head);
	head = modify(head, 6, 666);
	print(head);
}
List* Initlist(List* head, int key)
{head = (List*)malloc(sizeof(List));
	head->next = NULL;
	head->key = key;
	end = head;
	return head;
}

List* setList(List* head, int key)
{List* node = (List*)malloc(sizeof(List));
	node->key = key;
	node->next = NULL;
	end->next = node;
	end = node;
	return head;
}

int find(List* head, int key)
{int i = 0;
	while (head != NULL)
	{if(head->key==key)
		{	return i;
		}
		head = head->next;
		i++;
	}
	return -1;
}

List* modify(List* head, int key, int data)//key需要替換成data.
{int index = find(head, key);
	if (index == -1)
	{printf("Not Found");
		return;
	}
	else
	{List* temp = head;
		for (int i = 0; i< index; i++)
		{	head = head->next;
		}//指針移動(dòng)到目標(biāo)節(jié)點(diǎn).
		head->key = data;
		return temp;
	}
}

void print(List* head)
{while (head != NULL)
	{printf("%d ", head->key);
		head = head->next;
	}
	printf("\n");
}

List* insert(List* head, int key, int data)
{int index = find(head, key);
	if (index == -1)
	{printf("Not Found");
		return;
	}
	else
	{List* temp = head;
		for (int i = 0; i< index; i++)
		{	head = head->next;
		}//指針移動(dòng)到目標(biāo)節(jié)點(diǎn).
		List* node = (List*)malloc(sizeof(List));
		node->next = head->next;
		head->next = node;
		node->key = data;
		return temp;
	}
}

List* delete_node(List* head, int key)
{int index = find(head, key);
	if (index == -1)
	{printf("Not Found");
		return;
	}
	else
	{if (head->key == key)
		{	List* temp = head->next;
			free(head);
			return temp;
		}
		else
		{	List* temp = head;
			while(temp->next != NULL)//移動(dòng)到目標(biāo)節(jié)點(diǎn)前一個(gè)節(jié)點(diǎn)
			{		if (temp->next->key == key)
				{break;
				}
				temp = temp->next;
			}
			List* free_node = temp->next;
			temp->next = temp->next->next;
			free(free_node);
			return head;
		}
	}
}

運(yùn)行結(jié)果為
在這里插入圖片描述

總結(jié)

新人第一次寫博客,如有錯(cuò)誤,可以聯(lián)系作者改正,如有更好的方式,也歡迎交流(?′?`?)

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)頁(yè)標(biāo)題:【C·數(shù)據(jù)結(jié)構(gòu)】詳細(xì)實(shí)現(xiàn)單鏈表代碼+圖例直觀理解-創(chuàng)新互聯(lián)
瀏覽地址:http://m.newbst.com/article20/cejgco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)靜態(tài)網(wǎng)站網(wǎng)站建設(shè)商城網(wǎng)站用戶體驗(yàn)小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)