Data Structure BASICS (9)-Design and Implementation of a single-chain table (2) Advanced Operations
Link of the linked list:
After all the content of the second linked list is linked to the first linked list, the complete implementation code and analysis are as follows:
// Linked template of the linked list
Void MyList
: Concatenate (const MyList
& List) {if (isEmpty () // if your linked list is empty {first = list. first; return;} else if (list. isEmpty () // if the second linked list is empty {return;} Node
* EndNode = first-> next; // locate the End Node of the first linked list while (endNode-> next! = NULL) {endNode = endNode-> next;} // locate the first real element Node of the second linked list
* SecondListNode = (list. first)-> next; // note: copy the element values in the second linked list // you cannot directly link the header of the second linked list to the end of the first linked list. // otherwise, an error occurs when the Destructor recycles the memory (that is: the same memory is released twice) while (secondListNode! = NULL) {Node
* NewNode = new Node
(SecondListNode-> data); newNode-> next = NULL; endNode-> next = newNode; // two linked lists move forward simultaneously. endNode = endNode-> next; secondListNode = secondListNode-> next ;}}
Reverse of the linked list:
Basic Idea:
Traverse the linked list and use an auxiliary pointer (here the pointer r) to store the next element pointed to by the current pointer during the traversal process. After the pointer of the current node element is reversed, use the stored pointer to traverse the backend.
// Reverse template of the linked list
Void MyList
: Invort () {if (! IsEmpty () {// p points to the first real Node of the forward linked list // then, p will traverse along the square to the end of the linked list Node
* P = first-> next; // q will become the first real node in the backward direction. // set q to NULL first: ensure that the pointer field of the last element points to NULL after the reverse direction, to indicate that the linked list ends Node
* Q = NULL; while (p! = NULL) {Node
* R = q; // Save the node currently pointed to by q. // q is backward (backward along the forward direction) q = p; // p is forward (forward along the forward direction ), ensure that p is always ahead of q. p = p-> next; // reverse the pointer. // Note: This statement must be run after p pointer is moved, // otherwise p won't be able to go... (Because q changes the pointer orientation) q-> next = r ;} // at this time, q becomes the first real element of the reverse linked list. // However, to maintain the first pointer as before, point to a useless node (so that the previous operation will not go wrong) // so we need to direct the first pointer field to q first-> next = q ;}}
Chain table printing:
Reload the <operator of MyList to output all elements of the linked list for testing.
// Display all data in the Linked List (for test) template
Ostream & operator <(ostream & OS, const MyList
& List) {for (Node
* SearchNode = list. first-> next; searchNode! = NULL; searchNode = searchNode-> next) {OS <searchNode-> data; if (searchNode-> next! = NULL) // The End Of The linked list cout <"->" ;}return OS ;}
Appendix-test code:
int main(){ cout << "------------ 1 ------------" << endl; MyList
first; for (int i = 0; i < 5; ++i) { first.insert(i+1, i+1); } first.remove(5); MyList
second; for (int i = 0; i < 5; ++i) { second.insert(i+6, i+1); } second.insertFront(5); second.insert(88, 7); cout << "Before concatenate..." << endl; cout << "first: " << first << endl; cout << "second: " << second << endl; cout << "After concatenate..." << endl; first.concatenate(second); cout << "first: " << first << endl; cout << "second: " << second << endl; cout << "\n------------ 2 ------------" << endl; MyList
chList; for (char ch = '0'; ch <= '9'; ++ ch) { chList.insertFront(ch); } cout << "Before invort..." << endl; cout << chList << endl; cout << "After invort..." << endl; chList.invort(); cout << chList << endl; cout << "After remove('5')..." << endl; chList.remove('5'); cout << chList << endl; cout << "\n------------ 3 ------------" << endl; MyList
dList; dList.insert(1.1, 1); dList.insertFront(2.2); cout << dList << endl; return 0;}