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;
/**
*鏈表通用類型
*ElemType 代表自定義的數(shù)據(jù)類型 
*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í)有所幫助。
上一篇:C++加密解密php代碼的方法
欄 目:C語(yǔ)言
下一篇:C++實(shí)現(xiàn)八個(gè)常用的排序算法:插入排序、冒泡排序、選擇排序、希爾排序等
本文標(biāo)題:C語(yǔ)言之單鏈表的插入、刪除與查找
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2942.html
您可能感興趣的文章
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用函數(shù)刪除字符
 - 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)式函數(shù)庫(kù)
 - 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
 - 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
 - 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段函數(shù)
 - 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排序法函數(shù)
 - 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
 - 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段函數(shù)
 - 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
 - 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求階乘
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
 - 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
 - 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
 - 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
 - 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
 - 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
 - 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
 - 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
 - 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
 - 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
 - 01-10delphi制作wav文件的方法
 - 04-02jquery與jsp,用jquery
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 


