C語(yǔ)言之單鏈表的插入、刪除與查找
單鏈表是一種鏈?zhǔn)酱嫒〉臄?shù)據(jù)結(jié)構(gòu),用一組地址任意的存儲(chǔ)單元存放線性表中的數(shù)據(jù)元素。要實(shí)現(xiàn)對(duì)單鏈表中節(jié)點(diǎn)的插入、刪除與查找的功能,就要先進(jìn)行的單鏈表的初始化、創(chuàng)建和遍歷,進(jìn)而實(shí)現(xiàn)各功能,以下是對(duì)單鏈表節(jié)點(diǎn)的插入、刪除、查找功能的具體實(shí)現(xiàn):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int ElemType;
/**
*鏈表通用類(lèi)型
*ElemType 代表自定義的數(shù)據(jù)類(lèi)型
*struct Node *next 代表 結(jié)構(gòu)體指針(指向下一個(gè)結(jié)構(gòu)體,完成鏈表動(dòng)作)
*/
typedef struct Node{
ElemType data;
struct Node *next;
}Node;
/*==========單鏈表的初始化================*/
/*
*頭結(jié)點(diǎn)指針數(shù)據(jù)域設(shè)置為空
*/
void initList(Node **pNode){
*pNode=NULL;
}
/*===========單鏈表的創(chuàng)建=================*/
/*
*功能實(shí)現(xiàn):通過(guò)用戶不斷輸入數(shù)據(jù),創(chuàng)建鏈表
*利用游標(biāo)倆個(gè)指針(p1,p2),將申請(qǐng)下的數(shù)據(jù)塊(存入用戶輸入數(shù)據(jù)),鏈接起來(lái)
*/
Node *create(Node *pHead){
Node *p1;
Node *p2;
p1=p2=(Node *)malloc(sizeof(Node)); //申請(qǐng)內(nèi)存空間
memset(p1,0,sizeof(Node)); //存入數(shù)據(jù)域清空
scanf("%d",&p1->data);
p1->next=NULL;
while(p1->data>0){ //輸入負(fù)數(shù)結(jié)束
if(pHead==NULL)
pHead=p1;
else
p2->next=p1;
p2=p1;
p1=(Node *)malloc(sizeof(Node));
memset(p1,0,sizeof(Node));
scanf("%d",&p1->data);
p1->next=NULL;
}
return pHead;
}
/*=================鏈表的遍歷==================*/
/**
*從頭結(jié)點(diǎn)開(kāi)始,不斷遍歷出數(shù)據(jù)域的內(nèi)容將表遍歷
*/
void printList(Node *pHead){
if(NULL==pHead)
printf("鏈表為空\(chéng)n");
else{
while(pHead!=NULL){
printf("%d ",pHead->data);
pHead=pHead->next;
}
}
printf("\n");
}
/*===============插入節(jié)點(diǎn)==================*/
/**
*Node **pNode 傳入頭結(jié)點(diǎn)空間地址
*int i 傳入要插入的結(jié)點(diǎn)位置
*/
void insert_data(Node **pNode,int i){
Node *temp;
Node *target;
Node *p;
int item;
int j=1;
printf("輸入要插入的節(jié)點(diǎn)值:");
scanf("%d",&item);
target=*pNode;
for(;j<i-1;target=target->next,++j); //不斷移動(dòng)target位置,到要插入結(jié)點(diǎn)位置,
temp=(Node *)malloc(sizeof(Node)); //申請(qǐng)內(nèi)存空間
temp->data=item; //存入要存入的數(shù)據(jù)位置
p=target->next;
target->next=temp;
temp->next=p;
}
/*===============刪除節(jié)點(diǎn)====================*/
/**
*刪除結(jié)點(diǎn)后,釋放內(nèi)存空間free(temp)
*/
void delete_data(Node **pNode,int i){
Node *target;
Node *temp;
int j=1;
target=*pNode;
for(;j<i-1;target=target->next,++j);
temp=target->next;
target->next=temp->next;
free(temp);
}
/*===============查找結(jié)點(diǎn)====================*/
int search_data(Node *pNode,int elem){
Node *target;
int i=1;
for(target=pNode;target->data!=elem && target->next!=NULL;++i,target=target->next);
if(target->next==NULL)
return 0;
else
return i;
}
int main(){
int i;
Node *pHead=NULL;
initList(&pHead);
pHead=create(pHead);
printList(pHead);
printf("輸入插入節(jié)點(diǎn)位置\n");
scanf("%d",&i);
insert_data(&pHead,i);
printList(pHead);
printf("輸入刪除節(jié)點(diǎn)位置\n");
scanf("%d",&i);
delete_data(&pHead,i);
printList(pHead);
printf("輸入查找節(jié)點(diǎn)\n");
scanf("%d",&i);
printf("節(jié)點(diǎn)所在位置:%d",search_data(pHead,i));
return 0;
}

通過(guò)以上各功能的實(shí)現(xiàn),希望對(duì)大家單鏈表的學(xué)習(xí)有所幫助。
相關(guān)文章
C/C++實(shí)現(xiàn)枚舉網(wǎng)上鄰居信息的示例詳解
在Windows系統(tǒng)中,通過(guò)網(wǎng)絡(luò)鄰居可以方便地查看本地網(wǎng)絡(luò)中的共享資源和計(jì)算機(jī),本文將介紹一個(gè)簡(jiǎn)單的C++程序,使用Windows API枚舉網(wǎng)絡(luò)鄰居信息,并獲取對(duì)端名稱(chēng)、本機(jī)名稱(chēng)、主機(jī)名稱(chēng)以及主機(jī)IP等信息,文中通過(guò)代碼示例給大家講解非詳細(xì),需要的朋友可以參考下2023-12-12
C語(yǔ)言關(guān)鍵字之a(chǎn)uto register詳解
這篇文章主要為大家介紹了C語(yǔ)言關(guān)鍵字之a(chǎn)uto register,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01
C++?線段樹(shù)原理與實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了C++?線段樹(shù)原理與實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Visual Studio 2019 如何新建 Win32項(xiàng)目的方法步驟
這篇文章主要介紹了Visual Studio 2019 如何新建 Win32項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
C語(yǔ)言用easyx實(shí)現(xiàn)消磚塊游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言消磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
數(shù)據(jù)結(jié)構(gòu)之AVL樹(shù)詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之AVL樹(shù)詳解,本文非常細(xì)致的講解了AVL樹(shù)的基礎(chǔ)知識(shí)、AVL樹(shù)的旋轉(zhuǎn)操作、AVL數(shù)的插入和刪除操作等,需要的朋友可以參考下2014-08-08
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易通訊錄實(shí)例
大家好,本篇文章主要講的是C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易通訊錄實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02

