当前位置:   article > 正文

C语言实现去除链表中的重复元素(带头节点)_6-4 链表去重 分数 10 作者 黄龙军 单位 绍兴文理学院 要求实现函数,将带头结点的

6-4 链表去重 分数 10 作者 黄龙军 单位 绍兴文理学院 要求实现函数,将带头结点的
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node{
  4. int Data;
  5. struct Node *Next;
  6. };
  7. void insert_tail(struct Node *L,int num){/*尾插法*/
  8. struct Node *p,*q;
  9. p=L;
  10. while(p->Next!=NULL){
  11. p=p->Next;
  12. }
  13. q=(struct Node *)malloc(sizeof(struct Node));
  14. q->Data=num;
  15. q->Next=p->Next;
  16. p->Next=q;
  17. }
  18. void show(struct Node *L){/*打印链表*/
  19. struct Node *p;
  20. p=L;
  21. while(p->Next!=NULL){
  22. p=p->Next;
  23. printf("%d\t",p->Data);
  24. }
  25. printf("\n");
  26. }
  27. void slim(struct Node *L){/*去除链表中重复元素*/
  28. struct Node *p,*q;
  29. p=L->Next;
  30. while(p!=NULL){
  31. while(p->Next!=NULL&&p->Data==p->Next->Data){
  32. q=p->Next;
  33. p->Next=q->Next;
  34. free(q);/*回收多余空间*/
  35. }
  36. p=p->Next;
  37. }
  38. }
  39. int main(){
  40. struct Node *L,*Neg;
  41. L=(struct Node *)malloc(sizeof(struct Node));
  42. L->Next=NULL;
  43. int i;
  44. for(i=1;i<=4;i++){/*输入构造含重复元素的链表*/
  45. insert_tail(L,i);
  46. insert_tail(L,i);
  47. insert_tail(L,i);
  48. }
  49. show(L);/*打印原有链表*/
  50. slim(L);
  51. show(L);/*打印去除重复元素后的链表*/
  52. return 0;
  53. }


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/594388
推荐阅读
相关标签
  

闽ICP备14008679号