C++链表操作实际应用技巧分享

开发 后端
我们在这里将会以一段代码示例来为大家详细讲解有关C++链表操作的实现方法,希望大家可以从中掌握到其中的应用技巧。

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++枚举类型用途及定义详解
责任编辑:曹凯 来源: 博客园
相关推荐

2010-02-01 11:13:00

C++ Traits

2010-02-06 13:47:08

C++标准扩展

2010-02-04 14:58:06

C++内存分配

2010-03-01 13:06:49

WCF继承

2010-02-05 13:44:06

C++ eof()函数

2010-02-06 16:16:01

C++冒泡排序

2010-02-05 18:04:21

C++剪切板

2011-07-13 16:36:11

C++

2010-02-03 15:35:00

C++输入输出汉字

2010-03-03 16:25:41

Python字符串显示

2010-03-01 17:52:03

WCF选择绑定

2010-02-05 17:25:26

C++标识符命名规则

2010-02-06 13:52:39

C++ profile

2010-02-04 11:15:28

C++模板限制

2010-02-06 10:24:48

C++二维数组初始化

2009-08-25 17:02:20

C#串口操作

2009-08-19 11:28:41

C#操作Word

2010-02-06 17:09:29

C++文件拷贝

2010-03-26 16:17:24

Python嵌入

2010-02-04 11:38:43

C++获取当前路径
点赞
收藏

51CTO技术栈公众号