龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

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

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
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.1fn",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:%ldn",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++链表操作的相关实现方法就为大家介绍到这里。

精彩图集

赞助商链接