本篇文章主要介紹了從單鏈表的創(chuàng)建、遍歷到節(jié)點的插入、刪除與查找功能的實現(xiàn),有需要的朋友可以參考下
單鏈表是一種鏈?zhǔn)酱嫒〉臄?shù)據(jù)結(jié)構(gòu),用一組地址任意的存儲單元存放線性表中的數(shù)據(jù)元素。要實現(xiàn)對單鏈表中節(jié)點的插入、刪除與查找的功能,就要先進行的單鏈表的初始化、創(chuàng)建和遍歷,進而實現(xiàn)各功能,以下是對單鏈表節(jié)點的插入、刪除、查找功能的具體實現(xiàn):
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- typedef int ElemType;
- /**
- *鏈表通用類型
- *ElemType 代表自定義的數(shù)據(jù)類型
- *struct Node *next 代表 結(jié)構(gòu)體指針(指向下一個結(jié)構(gòu)體,完成鏈表動作)
- */
- typedef struct Node{
- ElemType data;
- struct Node *next;
- }Node;
- /*==========單鏈表的初始化================*/
- /*
- *頭結(jié)點指針數(shù)據(jù)域設(shè)置為空
- */
- void initList(Node **pNode){
- *pNode=NULL;
- }
- /*===========單鏈表的創(chuàng)建=================*/
- /*
- *功能實現(xiàn):通過用戶不斷輸入數(shù)據(jù),創(chuàng)建鏈表
- *利用游標(biāo)倆個指針(p1,p2),將申請下的數(shù)據(jù)塊(存入用戶輸入數(shù)據(jù)),鏈接起來
- */
- Node *create(Node *pHead){
- Node *p1;
- Node *p2;
- p1=p2=(Node *)malloc(sizeof(Node)); //申請內(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é)點開始,不斷遍歷出數(shù)據(jù)域的內(nèi)容將表遍歷
- */
- void printList(Node *pHead){
- if(NULL==pHead)
- printf("鏈表為空/n");
- else{
- while(pHead!=NULL){
- printf("%d ",pHead->data);
- pHead=pHead->next;
- }
- }
- printf("/n");
- }
- /*===============插入節(jié)點==================*/
- /**
- *Node **pNode 傳入頭結(jié)點空間地址
- *int i 傳入要插入的結(jié)點位置
- */
- void insert_data(Node **pNode,int i){
- Node *temp;
- Node *target;
- Node *p;
- int item;
- int j=1;
- printf("輸入要插入的節(jié)點值:");
- scanf("%d",&item);
- target=*pNode;
- for(;j<i-1;target=target->next,++j); //不斷移動target位置,到要插入結(jié)點位置,
- temp=(Node *)malloc(sizeof(Node)); //申請內(nèi)存空間
- temp->data=item; //存入要存入的數(shù)據(jù)位置
- p=target->next;
- target->next=temp;
- temp->next=p;
- }
- /*===============刪除節(jié)點====================*/
- /**
- *刪除結(jié)點后,釋放內(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é)點====================*/
- 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é)點位置/n");
- scanf("%d",&i);
- insert_data(&pHead,i);
- printList(pHead);
- printf("輸入刪除節(jié)點位置/n");
- scanf("%d",&i);
- delete_data(&pHead,i);
- printList(pHead);
- printf("輸入查找節(jié)點/n");
- scanf("%d",&i);
- printf("節(jié)點所在位置:%d",search_data(pHead,i));
- return 0;
- }
通過以上各功能的實現(xiàn),希望對大家單鏈表的學(xué)習(xí)有所幫助。
新聞熱點
疑難解答