文章目錄
成都創(chuàng)新互聯(lián)歡迎聯(lián)系:18982081108,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),成都創(chuàng)新互聯(lián)網(wǎng)頁(yè)制作領(lǐng)域10余年,包括成都軟裝設(shè)計(jì)等多個(gè)方面擁有豐富的網(wǎng)站維護(hù)經(jīng)驗(yàn),選擇成都創(chuàng)新互聯(lián),為企業(yè)保駕護(hù)航!一,鏈表的概念
二,靜態(tài)創(chuàng)建鏈表和動(dòng)態(tài)遍歷
三,統(tǒng)計(jì)鏈表節(jié)點(diǎn)個(gè)數(shù)及鏈表查找
四,鏈表的插入
1,從指定節(jié)點(diǎn)后方插入新節(jié)點(diǎn)
2,從指定節(jié)點(diǎn)前方插入新節(jié)點(diǎn)?
五,鏈表刪除指定節(jié)點(diǎn)
六,動(dòng)態(tài)創(chuàng)建鏈表
?1,頭插法:
?2,尾插法:
1,什么是鏈表?
鏈表是一種數(shù)據(jù)結(jié)構(gòu),是一種數(shù)據(jù)存放的思想;
2,鏈表和數(shù)組的區(qū)別
數(shù)組的特點(diǎn):
鏈表的特點(diǎn):? ? ? ? ? ? ? ?
*兩者對(duì)比:
#includestruct Test//聲明結(jié)構(gòu)體類(lèi)型
{
int data;//定義數(shù)據(jù)域
struct Test *next;//定義指針域
};
//遍歷鏈表
void printLink(struct Test *head)
{
struct Test *p = head;//使p指向鏈表頭
while(p != NULL){//p現(xiàn)在是鏈表頭,會(huì)一直遍歷鏈表尾NULL時(shí),循環(huán)結(jié)束
printf("%d ",p->data);//輸出當(dāng)前節(jié)點(diǎn)中data的值
p = p->next;//使p指向下一個(gè)節(jié)點(diǎn)
}
printf("\n");
}
int main()
{
struct Test* head;
//定義結(jié)構(gòu)體變量,作為節(jié)點(diǎn),給節(jié)點(diǎn)賦值
struct Test t1 = {1,NULL};//對(duì)節(jié)點(diǎn)t1的data和next賦值
struct Test t2 = {2,NULL};//對(duì)節(jié)點(diǎn)t2的data和next賦值
struct Test t3 = {3,NULL};//對(duì)節(jié)點(diǎn)t3的data和next賦值
struct Test t4 = {4,NULL};//對(duì)節(jié)點(diǎn)t4的data和next賦值
struct Test t5 = {5,NULL};//對(duì)節(jié)點(diǎn)t5的data和next賦值
head = &t1;//將節(jié)點(diǎn)t1的起始地址賦給頭指針head
t1.next = &t2;//將節(jié)點(diǎn)t2的起始地址賦給t1節(jié)點(diǎn)中的next
t2.next = &t3;//將節(jié)點(diǎn)t3的起始地址賦給t2節(jié)點(diǎn)中的next
t3.next = &t4;//將節(jié)點(diǎn)t4的起始地址賦給t3節(jié)點(diǎn)中的next
t4.next = &t5;//將節(jié)點(diǎn)t5的起始地址賦給t4節(jié)點(diǎn)中的next
printLink(head);//把鏈表頭傳到函數(shù)printLink中
return 0;
}
?三,統(tǒng)計(jì)鏈表節(jié)點(diǎn)個(gè)數(shù)及鏈表查找#includestruct Test//聲明結(jié)構(gòu)體類(lèi)型
{
int data;//定義數(shù)據(jù)域
struct Test *next;//定義指針域
};
//遍歷鏈表
void printLink(struct Test *head)
{
struct Test *p = head;//使p指向鏈表頭
while(p != NULL){//p現(xiàn)在是鏈表頭,會(huì)一直遍歷鏈表尾NULL時(shí),循環(huán)停止
printf("%d ",p->data);//輸出當(dāng)前節(jié)點(diǎn)中data的值
p = p->next;//使p指向下一個(gè)節(jié)點(diǎn)
}
}
//統(tǒng)計(jì)節(jié)點(diǎn)的個(gè)數(shù)
int getNodeSum(struct Test *head)
{
int sum = 0;
struct Test *p = head;//使p指向鏈表頭
while(p != NULL){//遍歷鏈表,直到鏈表尾NULL時(shí)停止
sum++;//統(tǒng)計(jì)鏈表節(jié)點(diǎn)的個(gè)數(shù)
p = p->next;//使p指向下一個(gè)節(jié)點(diǎn)
}
return sum;//把統(tǒng)計(jì)的節(jié)點(diǎn)的個(gè)數(shù)return回去
}
//鏈表查詢(xún)
int searchLink(struct Test *head,int n)
{
while(head != NULL){//遍歷鏈表,直到鏈表尾NULL時(shí)停止
if(head->data == n){//判斷當(dāng)前節(jié)點(diǎn)中的data(數(shù)據(jù)域)和要查詢(xún)的是否相同
return 1;//相同的話return 1
}
head = head->next;//使head指向下一個(gè)節(jié)點(diǎn)
}
return 0;//不相同return 0
}
int main()
{
int ret;
struct Test* head;
//定義結(jié)構(gòu)體變量,作為節(jié)點(diǎn),給節(jié)點(diǎn)賦值
struct Test t1 = {1,NULL};//對(duì)節(jié)點(diǎn)t1的data和next賦值
struct Test t2 = {2,NULL};//對(duì)節(jié)點(diǎn)t2的data和next賦值
struct Test t3 = {3,NULL};//對(duì)節(jié)點(diǎn)t3的data和next賦值
struct Test t4 = {4,NULL};//對(duì)節(jié)點(diǎn)t4的data和next賦值
struct Test t5 = {5,NULL};//對(duì)節(jié)點(diǎn)t5的data和next賦值
head = &t1;//將節(jié)點(diǎn)t1的起始地址賦給頭指針head
t1.next = &t2;//將節(jié)點(diǎn)t2的起始地址賦給t1節(jié)點(diǎn)中的next
t2.next = &t3;//將節(jié)點(diǎn)t3的起始地址賦給t2節(jié)點(diǎn)中的next
t3.next = &t4;//將節(jié)點(diǎn)t4的起始地址賦給t3節(jié)點(diǎn)中的next
t4.next = &t5;//將節(jié)點(diǎn)t5的起始地址賦給t4節(jié)點(diǎn)中的next
printLink(head);//把鏈表頭傳到函數(shù)printLink中
ret = getNodeSum(head);
printf("此鏈表一共有%d個(gè)節(jié)點(diǎn)\n",ret);
ret = searchLink(head,2);
if(ret == 1){//判斷return回來(lái)的值
printf("有1這個(gè)節(jié)點(diǎn)\n");
}
else{
printf("沒(méi)有1這個(gè)節(jié)點(diǎn)\n");
}
ret = searchLink(head,7);
if(ret == 1){//判斷return回來(lái)的值
printf("有7這個(gè)節(jié)點(diǎn)\n");
}
else{
printf("沒(méi)有7這個(gè)節(jié)點(diǎn)\n");
}
return 0;
}
四,鏈表的插入插入一個(gè)新節(jié)點(diǎn)有兩種方法:? ? ? ??
找到指定節(jié)點(diǎn),把新節(jié)點(diǎn)節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)放在要新節(jié)點(diǎn)的next(指針域)中,再把新節(jié)點(diǎn)放在指定節(jié)點(diǎn)的next(指針域)中
#includestruct Test//聲明結(jié)構(gòu)體類(lèi)型
{
int data;//定義數(shù)據(jù)域
struct Test *next;//定義指針域
};
//遍歷鏈表
void printLink(struct Test *head)
{
struct Test *p = head;
while(p != NULL){//p現(xiàn)在是鏈表頭,會(huì)一直遍歷鏈表尾NULL時(shí)停止
printf("%d ",p->data);//輸出p節(jié)點(diǎn)中data的值
p = p->next;//使p指向下一節(jié)點(diǎn)
}
printf("\n");
}
//從指定節(jié)點(diǎn)后方插入新節(jié)點(diǎn)
void insertFromBehind(struct Test *head,int n,struct Test *new)
{
struct Test *p = head;
while(p != NULL){
if(p->data == n){//判斷當(dāng)前節(jié)點(diǎn)是不是指定節(jié)點(diǎn)
new->next = p->next;//把新節(jié)點(diǎn)的next(指針域)指向指定節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)(這邊要注意順序不能換,否則鏈表會(huì)斷掉)
p->next = new;//再把指定節(jié)點(diǎn)的next(指針域)指向新節(jié)點(diǎn)
}
p = p->next;//使p指向下一節(jié)點(diǎn)
}
}
int main()
{
struct Test* head;
//定義結(jié)構(gòu)體變量,作為節(jié)點(diǎn),給節(jié)點(diǎn)賦值
struct Test t1 = {1,NULL};//對(duì)節(jié)點(diǎn)t1的data和next賦值
struct Test t2 = {2,NULL};//對(duì)節(jié)點(diǎn)t2的data和next賦值
struct Test t3 = {3,NULL};//對(duì)節(jié)點(diǎn)t3的data和next賦值
struct Test t4 = {4,NULL};//對(duì)節(jié)點(diǎn)t4的data和next賦值
struct Test t5 = {5,NULL};//對(duì)節(jié)點(diǎn)t5的data和next賦值
head = &t1;//將節(jié)點(diǎn)t1的起始地址賦給頭指針head
t1.next = &t2;//將節(jié)點(diǎn)t2的起始地址賦給t1節(jié)點(diǎn)中的next
t2.next = &t3;//將節(jié)點(diǎn)t3的起始地址賦給t2節(jié)點(diǎn)中的next
t3.next = &t4;//將節(jié)點(diǎn)t4的起始地址賦給t3節(jié)點(diǎn)中的next
t4.next = &t5;//將節(jié)點(diǎn)t5的起始地址賦給t4節(jié)點(diǎn)中的next
printLink(head);//把鏈表頭傳到函數(shù)printLink中
struct Test new = {100,NULL};//定義一個(gè)新節(jié)點(diǎn)
insertFromBehind(head,5,&new);//把鏈表頭,要插入的位置,和新節(jié)點(diǎn)的地址傳過(guò)去
printLink(head);//把鏈表頭傳過(guò)去,打印鏈表
return 0;
}
2,從指定節(jié)點(diǎn)前方插入新節(jié)點(diǎn)?要考慮兩種情況:
#includestruct Test//聲明結(jié)構(gòu)體類(lèi)型
{
int data;//定義數(shù)據(jù)域
struct Test *next;//定義指針域
};
//遍歷鏈表
void printLink(struct Test *head)
{
struct Test *p = head;
while(p != NULL){//p現(xiàn)在是鏈表頭,會(huì)一直遍歷鏈表尾NULL時(shí)停止
printf("%d ",p->data);//輸出p節(jié)點(diǎn)中data的值
p = p->next;//使p指向下一節(jié)點(diǎn)
}
printf("\n");
}
//從指定節(jié)點(diǎn)前方插入新節(jié)點(diǎn)
struct Test* insertFromfront(struct Test *head,int data,struct Test *new)
{
struct Test* p = head;
//在頭節(jié)點(diǎn)插入(鏈表頭會(huì)改變)
if(p->data == data){//判斷指定的節(jié)點(diǎn)是不是頭節(jié)點(diǎn)
new->next = p;//讓新節(jié)點(diǎn)的next(指針域)指向p
return new;//現(xiàn)在new成為新的鏈表頭了
}
//在中間節(jié)點(diǎn)插入
while(p->next != NULL){//因?yàn)檫@里是從中間節(jié)點(diǎn)插入,所以會(huì)從第二個(gè)節(jié)點(diǎn)開(kāi)始遍歷鏈表,直到鏈表尾NULL時(shí)停止
if(p->next->data == data){//判斷當(dāng)前節(jié)點(diǎn)是不是指定節(jié)點(diǎn)
new->next = p->next;//讓要插入節(jié)點(diǎn)的next(指針域)指向p->next(就是當(dāng)前節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn))
p->next = new;//在讓當(dāng)前節(jié)點(diǎn)next(指針域)指向要插入的節(jié)點(diǎn)new
return head;//再把鏈表頭給return回去
}
p = p->next;//使p指向下一節(jié)點(diǎn)
}
return head;
}
int main()
{
struct Test* head;
//定義結(jié)構(gòu)體變量,作為節(jié)點(diǎn),給節(jié)點(diǎn)賦值
struct Test t1 = {1,NULL};//對(duì)節(jié)點(diǎn)t1的data和next賦值
struct Test t2 = {2,NULL};//對(duì)節(jié)點(diǎn)t2的data和next賦值
struct Test t3 = {3,NULL};//對(duì)節(jié)點(diǎn)t3的data和next賦值
struct Test t4 = {4,NULL};//對(duì)節(jié)點(diǎn)t4的data和next賦值
struct Test t5 = {5,NULL};//對(duì)節(jié)點(diǎn)t5的data和next賦值
head = &t1;//將節(jié)點(diǎn)t1的起始地址賦給頭指針head
t1.next = &t2;//將節(jié)點(diǎn)t2的起始地址賦給t1節(jié)點(diǎn)中的next
t2.next = &t3;//將節(jié)點(diǎn)t3的起始地址賦給t2節(jié)點(diǎn)中的next
t3.next = &t4;//將節(jié)點(diǎn)t4的起始地址賦給t3節(jié)點(diǎn)中的next
t4.next = &t5;//將節(jié)點(diǎn)t5的起始地址賦給t4節(jié)點(diǎn)中的next
printLink(head);//將頭指針的地址傳到函數(shù)printLink中
struct Test new = {100,NULL};//定義一個(gè)新節(jié)點(diǎn)
head = insertFromfront(head,5,&new);//把鏈表頭,要插入的位置,和新節(jié)點(diǎn)的地址傳過(guò)去
printLink(head);//把鏈表頭傳過(guò)去,打印鏈表
return 0;
}
五,鏈表刪除指定節(jié)點(diǎn)要考慮兩種情況:
*注意如果鏈表是動(dòng)態(tài)創(chuàng)建的記得把刪除的這個(gè)節(jié)點(diǎn)給free掉
#includestruct Test//聲明結(jié)構(gòu)體類(lèi)型
{
int data;//定義數(shù)據(jù)域
struct Test *next;//定義指針域
};
//遍歷鏈表
void printLink(struct Test *head)
{
struct Test *p = head;
while(p != NULL){//p現(xiàn)在是鏈表頭,會(huì)一直遍歷鏈表尾NULL時(shí)停止
printf("%d ",p->data);//輸出p節(jié)點(diǎn)中data的值
p = p->next;//使p指向下一節(jié)點(diǎn)
}
printf("\n");
}
//刪除指定節(jié)點(diǎn)
struct Test* deleteNode(struct Test *head,int data)
{
struct Test* p = head;
//刪除第一個(gè)節(jié)點(diǎn)
if(p->data == data){//判斷要?jiǎng)h除的節(jié)點(diǎn)是不是頭節(jié)點(diǎn)
head = head->next;//讓p指向下一個(gè)節(jié)點(diǎn)
return head;//把新的鏈表頭傳回去
}
//刪除非第一個(gè)節(jié)點(diǎn)
while(p->next != NULL){//從第二個(gè)節(jié)點(diǎn)開(kāi)始遍歷鏈表
if(p->next->data == data){//判斷當(dāng)前節(jié)點(diǎn)是不是要?jiǎng)h除的節(jié)點(diǎn)
p->next = p->next->next;//把要?jiǎng)h除節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)的next(指針域)越過(guò)要?jiǎng)h除的節(jié)點(diǎn),然后指向要?jiǎng)h除節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
return head;//把鏈表頭傳回去
}
p = p->next;//使p指向下一節(jié)點(diǎn)
}
return head;
}
int main()
{
struct Test* head;
//定義結(jié)構(gòu)體變量,作為節(jié)點(diǎn),給節(jié)點(diǎn)賦值
struct Test t1 = {1,NULL};//對(duì)節(jié)點(diǎn)t1的data和next賦值
struct Test t2 = {2,NULL};//對(duì)節(jié)點(diǎn)t2的data和next賦值
struct Test t3 = {3,NULL};//對(duì)節(jié)點(diǎn)t3的data和next賦值
struct Test t4 = {4,NULL};//對(duì)節(jié)點(diǎn)t4的data和next賦值
struct Test t5 = {5,NULL};//對(duì)節(jié)點(diǎn)t5的data和next賦值
head = &t1;//將節(jié)點(diǎn)t1的起始地址賦給頭指針head
t1.next = &t2;//將節(jié)點(diǎn)t2的起始地址賦給t1節(jié)點(diǎn)中的next
t2.next = &t3;//將節(jié)點(diǎn)t3的起始地址賦給t2節(jié)點(diǎn)中的next
t3.next = &t4;//將節(jié)點(diǎn)t4的起始地址賦給t3節(jié)點(diǎn)中的next
t4.next = &t5;//將節(jié)點(diǎn)t5的起始地址賦給t4節(jié)點(diǎn)中的next
printLink(head);//將頭指針的地址傳到函數(shù)printLink中
printf("刪除指定節(jié)點(diǎn)后的鏈表:\n");
head = deleteNode(head,5);//把鏈表頭,和要?jiǎng)h除第幾個(gè)節(jié)點(diǎn)傳過(guò)去
printLink(head);//把鏈表頭傳過(guò)去,打印鏈表
return 0;
}
六,動(dòng)態(tài)創(chuàng)建鏈表動(dòng)態(tài)創(chuàng)建鏈表也是有兩種方法:
1,頭插法:每一次創(chuàng)建的新節(jié)點(diǎn)插在之前的鏈表頭之前,再讓新節(jié)點(diǎn)做為新的鏈表頭;
#include#includestruct Test//聲明結(jié)構(gòu)體類(lèi)型
{
int data;//定義數(shù)據(jù)域
struct Test *next;//定義指針域
};
//遍歷鏈表
void printLink(struct Test *head)
{
struct Test *p = head;
while(p != NULL){//p現(xiàn)在是鏈表頭,會(huì)一直遍歷鏈表尾NULL時(shí)停止
printf("%d ",p->data);//輸出p節(jié)點(diǎn)中data的值
p = p->next;//使p指向下一節(jié)點(diǎn)
}
printf("\n");
}
struct Test* insertFromHead(struct Test* head,struct Test* new)
{
if(head == NULL){//判斷鏈表是否為空
head = new;//如果為空把創(chuàng)建的新節(jié)點(diǎn)當(dāng)作鏈表頭
}else{
new->next = head;//當(dāng)鏈表不為空的時(shí)候,讓新節(jié)點(diǎn)插在鏈表頭的前面(稱(chēng)之為頭插法)
head = new;//再讓新節(jié)點(diǎn)成為鏈表頭
}
return head;
}
//動(dòng)態(tài)創(chuàng)建鏈表(頭插法)
struct Test* creatLink(struct Test* head)
{
struct Test* new;
while(1){
new = (struct Test*)malloc(sizeof(struct Test));//開(kāi)辟空間
new->next = NULL;
printf("input your ne node data:\n");
scanf("%d",&(new->data));//輸入節(jié)點(diǎn)的數(shù)據(jù)域(data)
if(new->data == 0){//判斷每次輸入的值是否為0,如果為0,停止創(chuàng)建新節(jié)點(diǎn)
printf("0 quit\n");
return head;
}
head = insertFromHead(head,new);
}
return head;
}
int main()
{
struct Test* head = NULL;
head = creatLink(head);
printLink(head);
return 0;
}
2,尾插法:如果鏈表為空,創(chuàng)建的第一個(gè)節(jié)點(diǎn)做為鏈表頭,然后每一次創(chuàng)建的新節(jié)點(diǎn)插在鏈表最后一個(gè)節(jié)點(diǎn)的指針域(next)中;
#include#includestruct Test//聲明結(jié)構(gòu)體類(lèi)型
{
int data;//定義數(shù)據(jù)域
struct Test *next;//定義指針域
};
//遍歷鏈表
void printLink(struct Test *head)
{
struct Test *p = head;
while(p != NULL){//p現(xiàn)在是鏈表頭,會(huì)一直遍歷鏈表尾NULL時(shí)停止
printf("%d ",p->data);//輸出p節(jié)點(diǎn)中data的值
p = p->next;//使p指向下一節(jié)點(diǎn)
}
printf("\n");
}
struct Test* insertBehind(struct Test* head,struct Test* new)
{
struct Test* p = head;
if(p == NULL){//判斷鏈表是否為空
return head = new;//如果為空把創(chuàng)建的新節(jié)點(diǎn)當(dāng)作鏈表頭
}
while(p->next != NULL){//遍歷到最后一個(gè)節(jié)點(diǎn)
p = p->next;//使p指向下一節(jié)點(diǎn)
}
p->next = new;//把新節(jié)點(diǎn)插入最后一個(gè)節(jié)點(diǎn)的指針域(next)中
return head;//把鏈表頭傳回去
}
//動(dòng)態(tài)創(chuàng)建鏈表(尾插法)
struct Test* creatLink(struct Test* head)
{
struct Test* new;
while(1){
new = (struct Test*)malloc(sizeof(struct Test));//開(kāi)辟空間
new->next = NULL;
printf("input your ne node data:\n");
scanf("%d",&(new->data));//輸入節(jié)點(diǎn)的數(shù)據(jù)域(data)
if(new->data == 0){//判斷每次輸入的值是否為0,如果為0,停止創(chuàng)建新節(jié)點(diǎn)
printf("0 quit\n");
return head;
}
head = insertBehind(head,new);
}
}
int main()
{
struct Test* head = NULL;
head = creatLink(head);
printLink(head);
}
寫(xiě)在最后:博主是一位在校中專(zhuān)生,剛學(xué)不久,實(shí)力有限,內(nèi)容僅供參考,有不對(duì)地方歡迎大神指出,一起討論,一起進(jì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)查看詳情吧
本文題目:C語(yǔ)言—鏈表-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://jinyejixie.com/article24/egoje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、動(dòng)態(tài)網(wǎng)站、軟件開(kāi)發(fā)、網(wǎng)站維護(hù)、標(biāo)簽優(yōu)化、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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)
猜你還喜歡下面的內(nèi)容