加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

链表创建,显示,删除和倒置

发布时间:2020-12-13 20:35:07 所属栏目:百科 来源:网络整理
导读:链表创建,显示,删除和倒置 #include"stdafx.h" #includestdio.h #includemalloc.h typedef struct data { int Data; struct data*next; }str; void Create(str**p) { *p=(str*)malloc( sizeof (str)); printf( "inputthedata:" ); scanf( "%d" ,((*p)-Data
  1. 链表创建,显示,删除和倒置
  2. #include"stdafx.h"
  3. #include<stdio.h>
  4. #include<malloc.h>
  5. typedefstructdata
  6. {
  7. intData;
  8. structdata*next;
  9. }str;
  10. voidCreate(str**p)
  11. {
  12. *p=(str*)malloc(sizeof(str));
  13. printf("inputthedata:");
  14. scanf("%d",&((*p)->Data));
  15. if((*p)->Data!=0)
  16. {
  17. Create(&((*p)->next));
  18. }
  19. else
  20. {
  21. (*p)->next=NULL;
  22. return;
  23. }
  24. }
  25. voidDisplayList(data*pList)
  26. {
  27. while(pList!=NULL)
  28. {
  29. printf("%d---",pList->Data);
  30. pList=pList->next;
  31. }
  32. }
  33. voidFreeList(data*pList)//释放链表
  34. {
  35. data*temp=pList;
  36. while(temp->Data!=0)
  37. {
  38. pList=pList->next;
  39. free(temp);
  40. temp=NULL;
  41. if(pList->next!=NULL)
  42. temp=pList;
  43. }
  44. }
  45. voidReverseList(data**pList)
  46. {
  47. data*p1=NULL,*p2=NULL,*p3=NULL;
  48. p1=*pList;
  49. p2=p1->next;
  50. while(p2!=NULL)
  51. {
  52. p3=p2->next;
  53. p2->next=p1;
  54. p1=p2;
  55. p2=p3;
  56. }
  57. (*pList)->next=NULL;
  58. (*pList)=p1;
  59. }
  60. voidmain()
  61. {
  62. data*p;
  63. Create(&p);
  64. ReverseList(&p);
  65. DisplayList(p);
  66. FreeList(p);
  67. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读