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:
- // Link. h
- # Include<Iostream>
- # Include<Ctime>
- Struct Node
- {
- Public:
- Node (): _ val (0), _ next (NULL)
- {
- }
- Node (int val): _ val (val), _ next (NULL)
- {
- }
- Node (int val, Node * next): _ val (val), _ next (next)
- {
- }
- ~ Node ()
- {
- If (_ next)
- Delete _ next;
- }
- Public:
- Int _ val;
- Node * _ next;
- };
- Typedef Node * LinkNode;
- Node * CreateLink (int len, intMAX_BOUND=100)
- {
- Srand (unsigned int) time (NULL ));
- LinkNodeHead=NewNode (-1 );
- LinkNodeTmp=Head;
- For (intI=0; I< Len; ++ I)
- {
- //TmpTmp= Tmp->_ Next=NewNode (rand () % MAX_BOUND );
- TmpTmp= Tmp->_ Next=NewNode (I );
- }
- Tmp->_ Next=NULL;
- Return head;
- }
- Void ExchLinkNode (const LinkNode head, int i1, int i2)
- {
- // The head cannot be exchanged.
- LinkNodePrenode1=NULL; // Save the previous node of node1.
- LinkNodePostnode1=NULL; // Save the last node of node1.
- LinkNodePrenode2=NULL; // Save the previous node of node2.
- LinkNodePostnode2=NULL; // Save the last node of node2.
- LinkNodeNode1=NULL; // Save the node to be switched
- LinkNodeNode2=NULL; // Save the node to be switched
- LinkNodeTmp=Head;
- // Locate two nodes
- While (tmp->_ Val! = I1) & (tmp! = NULL ))
- {
- TmpTmp= Tmp->_ Next;
- }
- If (Tmp= NULL)
- {
- Return;
- }
- Else
- {
- Node1=Tmp;
- }
- Tmp=Head;
- While (tmp->_ Val! = I2) & (tmp! = NULL ))
- {
- TmpTmp= Tmp->_ Next;
- }
- If (Tmp= NULL)
- {
- Return;
- }
- Else
- {
- Node2=Tmp;
- }
- // Cannot be exchanged with the header Node
- If (Node1= Head)
- {
- Return;
- }
- Else if (Node2= Head)
- {
- Return;
- }
- // You and yourself do not have to exchange
- If (Node1= Node2)
- {
- Return;
- }
- Tmp=Head;
- While (tmp->_ Next! = Node1)
- {
- TmpTmp= Tmp->_ Next;
- }
- Prenode1=Tmp;
- Tmp=Head;
- While (tmp->_ Next! = Node2)
- {
- TmpTmp= Tmp->_ Next;
- }