Interpret and analyze the C ++ linked list

Source: Internet
Author: User

The C ++ language is a good learning tool for learning data structures. It can fully understand the functions and uses of the C ++ linked list in C ++, therefore, it is easy to understand the C ++ description, and it will not be difficult to learn any language in the future.

The meaning of the exchange node of a single-chain table is: given a single-chain table, it is required to exchange any two nodes. Note that the first node of the linked list is not involved in node exchange. This seems simple, but it still requires some basic skills.

The key to this problem is to use four pointers to save the positions of the front and back nodes of the two exchange nodes. For detailed implementation, see the implementation source code. In fact, there is also a more clear logic implementation: as long as two pointers are used to save the previous node of the current two switching nodes.

  • Introduction to the design principles of C ++
  • Introduction to the C ++ Manual
  • In-depth analysis of C ++ standard library description
  • How can C ++ be used for C ++ development programs?
  • How to define C ++ Constructor

Delete the nodes to be switched in sequence, and insert the two nodes to be deleted in turn after the previous node of the record, that is to say, we can actually convert this process into two basic operations for the C ++ linked list. However, in this implementation, when the two exchange nodes are adjacent nodes, problems may occur. You need to handle them separately. You can obtain the specific cause by performing one operation manually. The next method is not provided here.

In the implementation code, it should be noted that the exchange C ++ linked list node transmits two exchange node pointers, but in order to test the simple implementation, replace the two nodes with the keyword Value Field of the node to be switched), and locate them in the C ++ linked list.

The specific implementation source code is:

 
 
  1. // Link. h
  2. # Include<Iostream> 
  3. # Include<Ctime> 
  4. Struct Node
  5. {
  6. Public:
  7. Node (): _ val (0), _ next (NULL)
  8. {
  9. }
  10. Node (int val): _ val (val), _ next (NULL)
  11. {
  12. }
  13. Node (int val, Node * next): _ val (val), _ next (next)
  14. {
  15. }
  16. ~ Node ()
  17. {
  18. If (_ next)
  19. Delete _ next;
  20. }
  21. Public:
  22. Int _ val;
  23. Node * _ next;
  24. };
  25. Typedef Node * LinkNode;
  26. Node * CreateLink (int len, intMAX_BOUND=100)
  27. {
  28. Srand (unsigned int) time (NULL ));
  29. LinkNodeHead=NewNode (-1 );
  30. LinkNodeTmp=Head;
  31. For (intI=0; I< Len; ++ I)
  32. {
  33. //TmpTmp= Tmp->_ Next=NewNode (rand () % MAX_BOUND );
  34. TmpTmp= Tmp->_ Next=NewNode (I );
  35. }
  36. Tmp->_ Next=NULL;
  37. Return head;
  38. }
  39. Void ExchLinkNode (const LinkNode head, int i1, int i2)
  40. {
  41. // The head cannot be exchanged.
  42. LinkNodePrenode1=NULL; // Save the previous node of node1.
  43. LinkNodePostnode1=NULL; // Save the last node of node1.
  44. LinkNodePrenode2=NULL; // Save the previous node of node2.
  45. LinkNodePostnode2=NULL; // Save the last node of node2.
  46. LinkNodeNode1=NULL; // Save the node to be switched
  47. LinkNodeNode2=NULL; // Save the node to be switched
  48. LinkNodeTmp=Head;
  49. // Locate two nodes
  50. While (tmp->_ Val! = I1) & (tmp! = NULL ))
  51. {
  52. TmpTmp= Tmp->_ Next;
  53. }
  54. If (Tmp= NULL)
  55. {
  56. Return;
  57. }
  58. Else
  59. {
  60. Node1=Tmp;
  61. }
  62. Tmp=Head;
  63. While (tmp->_ Val! = I2) & (tmp! = NULL ))
  64. {
  65. TmpTmp= Tmp->_ Next;
  66. }
  67. If (Tmp= NULL)
  68. {
  69. Return;
  70. }
  71. Else
  72. {
  73. Node2=Tmp;
  74. }
  75. // Cannot be exchanged with the header Node
  76. If (Node1= Head)
  77. {
  78. Return;
  79. }
  80. Else if (Node2= Head)
  81. {
  82. Return;
  83. }
  84. // You and yourself do not have to exchange
  85. If (Node1= Node2)
  86. {
  87. Return;
  88. }
  89. Tmp=Head;
  90. While (tmp->_ Next! = Node1)
  91. {
  92. TmpTmp= Tmp->_ Next;
  93. }
  94. Prenode1=Tmp;
  95. Tmp=Head;
  96. While (tmp->_ Next! = Node2)
  97. {
  98. TmpTmp= Tmp->_ Next;
  99. }

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.