# Data Structure interview II-common operations on two-way linked list tables, circular linked list, and ordered linked list

Source: Internet
Author: User
Data Structure interview II-common operations on two-way linked list tables, cyclic linked lists, and ordered linked lists

Note: The interview book has related exercises, but the ideas are relatively unclear and the layout is incorrect. The author has rewritten the related books and opinions for your reference.

The establishment of a two-way linked list is based on a single-chain table, with an additional pointer to the front-end back. Other operations are similar. Note that two pointers must be modified in the two-way linked list operation, especially in the insert or delete operation. One is the back pointer and the other is the next pointer.

1. Create a two-way linked list [Inserted above]

Note: 1) modify the first pointer (header pointer. 2) modify the back and next pointers.

// Insert the reverse header from the front...

`template<typename Type>nodeType<Type>*doublyLinkedList<Type>::buildListBackward(){       nodeType<Type> *newNode;        int num;       cout << " Enter a list of integer end with -999." << endl;       cin >> num;        while(num != -999)       {              //..add              newNode = new nodeType<Type>;              newNode->info = num;              newNode->next = NULL;              newNode->back = NULL;                           if(first == NULL)              {                     first = newNode;              }              else              {                     newNode->next = first;                     first->back = newNode;                     first = newNode;              }               cin >> num;       }       return first;}`

2. Insert nodes of the two-way linked list [This section considers to ensure the order after insertion and insert the sorting directly]

You still need to consider multiple factors when inserting a single-chain table to a node. It can be divided into the following categories:

1) The two-way linked list is empty, "prompt information", and return.

2) The two-way linked list is not empty. If the element value of the node to be inserted is smaller than that of the first node, you must modify the first pointer.

3) The two-way linked list is not empty. If the element value of the node to be inserted is the node in the center of the linked list, you need to modify the back and next directions.

4) The two-way linked list is not empty. After the inserted node is at the end of the node, modify the pointer of the last node.

`// Two-way non-circular linked list template <typename type> voiddoublyshortlist <type >:: insertnode (const type & insertitem) // new {nodetype <type> * current; nodetype <type> * trailcurrent; nodetype <type> * newnode; bool found = false; newnode = new nodetype <type>; newnode-> info = insertitem; newnode-> next = NULL; newnode-> back = NULL; // Case 1: empty table if (first = NULL) {First = newnode;} else {current = first; while (current! = NULL) {If (current-> info >= insertitem) {found = true; break;} else {trailcurrent = current; current = Current-> next ;}} // Case 2: If (current = first) {current-> next = newnode; newnode-> back = current;} else {// Case 3: intermediate node if (current! = NULL) {newnode-> next = trailcurrent-> next; Current-> back = newnode; trailcurrent-> next = newnode; newnode-> back = trailcurrent ;} else // case 4: before the last node {trailcurrent-> next = newnode; newnode-> back = trailcurrent;} // end case 4 }}}`

3. Delete nodes from a two-way linked list

Consider the following:

1) The linked list is empty, "prompt information", and return.

2) If the linked list is not empty, check whether the deleted element exists in the linked list. If yes, found = true; if no, found = false.

3) If no node contains the search element is found, "error prompt" is returned.

4) if you find the primary node. If the first or last node is the header node, the first pointer is changed, and the back and next directions of the node are changed. If the first or last node is the intermediate node, the back and next directions are the main nodes, insert.

`Template <typename type> void doublyinclulist <type>: deletenode (consttype & deleteitem) // new {nodetype <type> * Current = new nodetype <type>; nodetype <type> * trailcurrent; bool found = false; // Case 1: empty table if (first = NULL) {cout <"The list is null! "<Endl; exit (1) ;}else {current = first; while (current! = NULL) {If (current-> info = deleteitem) {found = true; break;} else {trailcurrent = current; current = Current-> next ;}} if (found) {// Case 2: first node if (current = first) {First = Current-> next; delete current;} else {// Case 3: intermediate node if (current! = NULL) {If (current-> next! = NULL) // case3.1: The intermediate node to be deleted. {trailcurrent-> next = Current-> next; Current-> next-> back = trailcurrent; delete current;} else {trailcurrent-> next = NULL; // case3.2: the last node to be deleted. delete current; }}// end else} // end if else {cout <"The ELEM" <deleteitem <"is not exist in the list! "<Endl; // case 4: this node is not found in the linked list.} // end else} // end`

The circular linked list ensures that the linked list can be retrieved from each node. That is, the last pointer is directed to the first node instead of null ).

When constructing a linked list, searching for elements, inserting, deleting, and operating a linked list, you must modify the pointer to the end node to ensure the cycle of the linked list.

1. Create a circular linked list using the forward plug-in

[Idea]: Insert a node in front of the first pointer each time. After each node is inserted, change the last-> link direction.

`Template <typename type> void cancelist <type>: bucket cyclistbackward () // The prefix constructs a circular linked list. {type newitem; while (CIN> newitem, newitem! =-999) {nodetype <type> * newnode = newnodetype <type>; newnode-> info = newitem; newnode-> link = NULL; If (first = NULL) {First = newnode; last = newnode; first-> link = first; last-> link = first;} else {newnode-> link = first; last-> link = newnode; first = newnode ;}}cout <"input complete! "<Endl ;}`

2. Insert elements in the circular linked list [insert later]

[Idea]: Construct a circular linked list and insert nodes after the last pointer each time. After inserting each node, modify the link pointing to the last-> link.

`// Insert only the template <typename type> voidreceivelist <type >:: insertpolicist (const type & newitem) {nodetype <type> * newnode = new nodetype <type>; newnode-> info = newitem; newnode-> link = NULL; If (first = NULL) // The linked list is empty... {First = newnode; last = newnode; first-> link = first; last-> link = first;} else // The linked list is not empty... {last-> link = newnode; newnode-> link = first; last = newnode;} cout <newitem <"was inserted! "<Endl ;}`

3. Delete cyclic linked list nodes

Considering whether the linked list is empty, whether the deletion element does not exist in the linked list, and whether the node exists (the node may be in the same position as the header, center, and end), the table can be processed in the following five cases. :

`// Case1: The linked list is empty. // Case2: The linked list is not empty. The deleted node is the first node. // Case3: The linked list is not empty. The deleted node is the end node. // Case4: The linked list is not empty, and the deleted node is an intermediate node. // Case5: The linked list is not empty. The node = deleteitem does not exist. Template <typename type> voidcyclelist <type>: delemediist (const type & deleteitem) {nodetype <type> * Current = new nodetype <type>; nodetype <type> * trailcurrent = new nodetype <type>; nodetype <type> * temp; bool found = false; If (first = NULL) {cout <"The list is empty! "<Endl; // case1 return;} If (first-> info = deleteitem) // case2 {temp = new nodetype <type>; temp = first; first = first-> link; last-> link = first; cout <"the node has" <deleteitem <"was deleted! "<Endl; Delete temp; return;} else // cas3, case4. locate it after search. {Current = first-> link; while (! Found & current! = First) {If (current-> info = deleteitem) {found = true; break;} else {trailcurrent = current; current = Current-> link ;}} // end while if (found) {temp = new nodetype <type>; If (current = last) // case3 {temp = last; trailcurrent-> link = last-> link; // last-> link = first last = trailcurrent;} else {temp = current; trailcurrent-> link = Current-> link ;} cout <"The ELEM:" <deleteitem <" Was deleted! "<Endl; Delete temp;} else {cout <" The ELEM: "<deleteitem <" was not exist in the list! "<Endl ;}// end else}`

Note: In this example, the ordered linked list is only based on the previous operations on a single-chain table. When inserting elements, insert them in sequence and sort them in consideration.

In order to reflect the characteristics of order, we use recursion to print the reverse order of order list. Non-Recursive Implementation of ordered or linked lists can be achieved through stacks. The next section will analyze and implement it.

1. Insert an ordered linked list

Consider the following three situations:

1) The current linked list is empty;

2) The current linked list is not empty. The element value to be inserted is smaller than the element value of the header node;

3) The current linked list is not empty. If the element value to be inserted is greater than the element value of the header node, you can find the first element greater than the element, and then stop searching. Just insert it before.

`Template <typename type> void orderedlinkedlisttype <type >:: insertnode (consttype & newitem) {nodetype <type> * Current; nodetype <type> * trailcurrent; nodetype <type> * newnode; bool found = false; newnode = new nodetype <type>; newnode-> info = newitem; newnode-> link = NULL; If (first = NULL) // case1: the linked list is empty. {First = newnode; last = first;} else {current = first; while (! Found & current! = NULL) // loop query {If (current-> info> = newitem) {found = true; break;} else {trailcurrent = current; current = Current-> link;} // end while // first special processing .. if (current = first) // case2: The newly inserted element is smaller than the element value of the first node .. {newnode-> link = first; first = newnode;} else // other .. {newnode-> link = current; // case3: The newly inserted node trailcurrent-> link = newnode; }}// end else} in a non-first position}`

2. recursively print the reverse order of the ordered linked list

`Template <typename type> voidorderedlinkedlisttype <type>: printlistreverse () const // print in reverse order. {reverseprint (first); cout <Endl ;}// recursively prints the reverse order of a single-chain table. template <typename type> voidorderedlinkedlisttype <type>: reverseprint (nodetype <type> * Current) const // print in reverse order {If (current! = NULL) {reverseprint (current-> link); cout <Current-> info <"\ t ";}}`

Related Keywords:

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.