首页 > 编程 > 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语言之单链表的插入、删除与查找

通过以上各功能的实现,希望对大家单链表的学习有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表