C 链表操作实际应用技巧分享
创始人
2024-06-22 09:40:42
0

C++编程语言应用范围广泛,在开发人员眼中,它占据着非常重要的地位。在这里我们可以通过对C++链表操作的相关技巧,来充分了解一下这一语言的应用方式以及他的应用能给我们带来哪些不同的感受。

C++链表操作代码示例:

  1. // linklist.cpp : 定义控制台应用程序的入口点。   
  2. #include "stdafx.h"   
  3. #include "malloc.h"   
  4. #include "stdlib.h"   
  5. #define NULL 0   
  6. #define LEN sizeof(struct student)   
  7. struct student   
  8. {   
  9. long num;   
  10. float score;   
  11. struct student* next;   
  12. };   
  13. int n;   
  14. struct student* creat()   
  15. {   
  16. struct student *head;   
  17. struct student *p1,*p2;   
  18. n=0;   
  19. p1=p2=(struct student*)malloc(LEN);   
  20. scanf("%ld",&p1->num);   
  21. scanf("%f",&p1->score);   
  22. head=NULL;   
  23. while (p1->num!=0)   
  24. {   
  25. n++;   
  26. if (n==1)   
  27. {   
  28. head=p1;   
  29. }   
  30. else   
  31. {   
  32. p2->next=p1;   
  33. }   
  34. p2=p1;   
  35. p1=(struct student*)malloc(LEN);   
  36. scanf("%ld",&p1->num);   
  37. if (p1->num==0)   
  38. break;   
  39. scanf("%f",&p1->score);   
  40. }   
  41. p2->next=NULL;   
  42. return (head);   
  43. };   
  44. void print(struct student *head)   
  45. {   
  46. struct student *p;   
  47. printf("\n New,These %d records are:\n",n);   
  48. p=head;   
  49. if (head!=NULL)   
  50. {   
  51. do   
  52. {   
  53. printf("%d %5.1f\n",p->num,p->score);   
  54. pp=p->next;   
  55. }while (p!=NULL);   
  56. }   
  57. }   
  58. struct student* insert(struct student *head,struct student *stud)   
  59. {   
  60. struct student *p0, *p1, *p2;   
  61. p1=head;   
  62. p0=stud;   
  63. if (head==NULL)   
  64. {   
  65. head=p0;   
  66. p0->next=NULL;//insert into head point   
  67. }   
  68. else   
  69. {   
  70. while ((p0->num>p1->num)&&(p1->next!=NULL))   
  71. {   
  72. p2=p1; //p2 is point to just p1 point to node;   
  73. p1p1=p1->next;   
  74. }   
  75. if (p0->num<=p1->num)   
  76. {   
  77. if (p1==head)   
  78. {   
  79. head=p0;//insert into before first node   
  80. }   
  81. else   
  82. {   
  83. p2->next=p0;//insert into after point p2   
  84. }   
  85. p0->next=p1;   
  86. }   
  87. else   
  88. {   
  89. p1->next=p0; //insert into after last point   
  90. p0->next=NULL;   
  91. }   
  92. }   
  93. n++;   
  94. return(head);   
  95. };   
  96. struct student* del(struct student *head,long num)   
  97. {   
  98. struct student *p1, *p2;   
  99. if (head==NULL)   
  100. {   
  101. printf("\n list Null!\n");   
  102. return (head);   
  103. }   
  104. p1=head;   
  105. while (num!=p1->num&&p1->next!=NULL)   
  106. //find num if equal p1->num   
  107. {   
  108. p2=p1;   
  109. p1p1=p1->next;   
  110. }   
  111. if (num==p1->num)   
  112. {   
  113. if (p1==head)   
  114. head=p1->next;//delete head node because num=head.num   
  115. else   
  116. p2->next=p1->next;//delete node. node is not head point   
  117. printf("delete:%ld\n",num);   
  118. n--;   
  119. }   
  120. else   
  121. {   
  122. printf("%ld not been found!\n",num);   
  123. }   
  124. return (head);   
  125. };   
  126. int _tmain(int argc, _TCHAR* argv[])   
  127. {   
  128. struct student *head,*end;   
  129. head=creat();   
  130. print(head);   
  131. struct student insertnode;   
  132. insertnode.num=3;   
  133. insertnode.score=900;   
  134. head=insert(head,&insertnode);   
  135. print(head);   
  136. head=del(head,3);   
  137. print(head);   
  138. return 0;   

C++链表操作的相关实现方法就为大家介绍到这里。

【编辑推荐】

  1. C++产生随机数具体实现方法详解
  2. C++打印地址信息实现方法介绍
  3. C++格式化字符串相关应用解析
  4. C++枚举子相关类型解析
  5. C++枚举类型用途及定义详解

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...