Practical application skills of C ++ linked list operations

Source: Internet
Author: User

The C ++ programming language is widely used and plays a very important role in the eyes of developers. Here, we can use the C ++ linked list related skills to fully understand the application methods of this language and how their applications can bring us different feelings.

C ++ linked list operation code example:

 
 
  1. // Linklist. cpp: defines the entry point of the console application.
  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;
  139. }

The implementation methods of C ++ linked list operations are described here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.