国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C語言之單鏈表的插入、刪除與查找

2020-05-23 14:17:35
字體:
來源:轉載
供稿:網友

本篇文章主要介紹了從單鏈表的創建、遍歷到節點的插入、刪除與查找功能的實現,有需要的朋友可以參考下

單鏈表是一種鏈式存取的數據結構,用一組地址任意的存儲單元存放線性表中的數據元素。要實現對單鏈表中節點的插入、刪除與查找的功能,就要先進行的單鏈表的初始化、創建和遍歷,進而實現各功能,以下是對單鏈表節點的插入、刪除、查找功能的具體實現:

 

 
  1. #include<stdio.h> 
  2. #include<stdlib.h> 
  3. #include<string.h> 
  4.  
  5. typedef int ElemType; 
  6.  
  7. /** 
  8. *鏈表通用類型 
  9. *ElemType 代表自定義的數據類型  
  10. *struct Node *next 代表 結構體指針(指向下一個結構體,完成鏈表動作)  
  11. */ 
  12. typedef struct Node{ 
  13. ElemType data; 
  14. struct Node *next; 
  15. }Node;  
  16.  
  17. /*==========單鏈表的初始化================*/ 
  18. /* 
  19. *頭結點指針數據域設置為空  
  20. */ 
  21. void initList(Node **pNode){ 
  22. *pNode=NULL; 
  23. /*===========單鏈表的創建=================*/ 
  24. /* 
  25. *功能實現:通過用戶不斷輸入數據,創建鏈表 
  26. *利用游標倆個指針(p1,p2),將申請下的數據塊(存入用戶輸入數據),鏈接起來  
  27. */ 
  28. Node *create(Node *pHead){ 
  29. Node *p1; 
  30. Node *p2; 
  31. p1=p2=(Node *)malloc(sizeof(Node)); //申請內存空間  
  32. memset(p1,0,sizeof(Node)); //存入數據域清空  
  33. scanf("%d",&p1->data); 
  34. p1->next=NULL;  
  35. while(p1->data>0){ //輸入負數結束  
  36. if(pHead==NULL) 
  37. pHead=p1; 
  38. else 
  39. p2->next=p1; 
  40. p2=p1; 
  41. p1=(Node *)malloc(sizeof(Node)); 
  42. memset(p1,0,sizeof(Node)); 
  43. scanf("%d",&p1->data); 
  44. p1->next=NULL; 
  45. return pHead; 
  46. /*=================鏈表的遍歷==================*/ 
  47. /** 
  48. *從頭結點開始,不斷遍歷出數據域的內容將表遍歷  
  49. */ 
  50. void printList(Node *pHead){ 
  51. if(NULL==pHead) 
  52. printf("鏈表為空/n"); 
  53. else
  54. while(pHead!=NULL){ 
  55. printf("%d ",pHead->data); 
  56. pHead=pHead->next; 
  57. }  
  58. printf("/n"); 
  59. }  
  60. /*===============插入節點==================*/ 
  61. /** 
  62. *Node **pNode 傳入頭結點空間地址 
  63. *int i 傳入要插入的結點位置  
  64. */ 
  65. void insert_data(Node **pNode,int i){ 
  66. Node *temp; 
  67. Node *target; 
  68. Node *p; 
  69. int item; 
  70. int j=1; 
  71. printf("輸入要插入的節點值:"); 
  72. scanf("%d",&item); 
  73. target=*pNode;  
  74. for(;j<i-1;target=target->next,++j); //不斷移動target位置,到要插入結點位置,  
  75. temp=(Node *)malloc(sizeof(Node)); //申請內存空間  
  76. temp->data=item; //存入要存入的數據位置  
  77. p=target->next;  
  78. target->next=temp; 
  79. temp->next=p;  
  80. }  
  81. /*===============刪除節點====================*/ 
  82. /** 
  83. *刪除結點后,釋放內存空間free(temp)  
  84. */ 
  85. void delete_data(Node **pNode,int i){ 
  86. Node *target; 
  87. Node *temp; 
  88. int j=1; 
  89. target=*pNode; 
  90. for(;j<i-1;target=target->next,++j); 
  91. temp=target->next; 
  92. target->next=temp->next; 
  93. free(temp); 
  94. /*===============查找結點====================*/ 
  95. int search_data(Node *pNode,int elem){ 
  96. Node *target; 
  97. int i=1; 
  98. for(target=pNode;target->data!=elem && target->next!=NULL;++i,target=target->next); 
  99. if(target->next==NULL) 
  100. return 0; 
  101. else 
  102. return i; 
  103.  
  104. }  
  105. int main(){ 
  106. int i; 
  107. Node *pHead=NULL; 
  108. initList(&pHead); 
  109. pHead=create(pHead); 
  110. printList(pHead); 
  111. printf("輸入插入節點位置/n"); 
  112. scanf("%d",&i); 
  113. insert_data(&pHead,i); 
  114. printList(pHead); 
  115. printf("輸入刪除節點位置/n"); 
  116. scanf("%d",&i); 
  117. delete_data(&pHead,i); 
  118. printList(pHead); 
  119. printf("輸入查找節點/n"); 
  120. scanf("%d",&i); 
  121. printf("節點所在位置:%d",search_data(pHead,i)); 
  122. return 0; 

C語言之單鏈表的插入、刪除與查找

通過以上各功能的實現,希望對大家單鏈表的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平泉县| 耒阳市| 辽阳市| 安丘市| 古田县| 夏邑县| 德庆县| 皮山县| 翁源县| 边坝县| 化州市| 双柏县| 察隅县| 五莲县| 安塞县| 噶尔县| 嘉鱼县| 池州市| 新晃| 平果县| 漳浦县| 竹山县| 西畴县| 宿州市| 淮滨县| 万盛区| 临漳县| 涟水县| 百色市| 卫辉市| 小金县| 宝鸡市| 青神县| 南通市| 静乐县| 中江县| 贵州省| 固原市| 葫芦岛市| 手机| 建平县|