Write a program that inserts a new node in a sorted circular list.
For example, suppose the initial loop list is as follows:
After inserting a new node 7, the above loop list becomes as follows:
algorithm:assuming that the new node that needs to be inserted is newnode, the insert operation can be divided into the following 3 scenarios:
1) The list is empty: a) because the loop list only newnode this one node, it is self-looping. Newnode->next = NewNode; b) Adjust the head pointer *head = newnode;2) The new node needs to be inserted in front of the node of the list head: (a) traverse the list to find the tail node. while (current->next! = *head) Current = current->next; (b) Modify the back pointer of the tail node. Current->next = NewNode; (c) Adjust the NewNode back pointer to the original head node. Newnode->next = *head; (d) Adjust the head pointer to point to the new node. *head = newnode;3) The new node needs to be inserted behind a node: (a) Locate the location where the node needs to be inserted. while (current->next!= *head && current->next->data < newnode->data) Current = current ->next; (b) Adjust the back pointer of the new node to the back pointer of the anchor node Newnode->next = current->next; (c) Modifying the position node's back pointer current->next = NewNode; Code Implementation#include <iostream>//linked list node struct node{int data; Node *next;}; void swap (int *a, int*b) {int tmp = *A;*A = *B;*B = tmp;} Insert a new node in a sorted circular link table void Sortedinsert (node** head, node* newNode) {node* current = *head;//Case 1if (now = = NULL) {NewNode ->next = Newnode;*head = NewNode;} Case 2else if (current->data >= newnode->data) {//If the new node value is less than the head node value, you need to adjust the tail node's back pointer while (Current->next! = *head) Current = Current->next;current->next = Newnode;newnode->next = *head;*head = NewNode;} Scenario 3else{//locating the node that needs to be inserted into the data while (current->next! = *head && Current->next->data < Newnode->data) Current = Current->next;newnode->next = Current->next;current->next = NewNode;}} Insert a new node in the loop list header void push (node **head, int data) {Node *newnode = new node; Node *temp = *head;newnode->data = Data;newnode->next = *head;//If the linked list is not empty, the last node's back pointer is set to the new node//That is, the new node becomes the new head node. if (*head! = NULL) {while (Temp->next! = *head) temp = Temp->next;temp->next = NewNode;} Elsenewnode->next = NewNode; The new node is made as the first node of the list *head = NewNode; Adjust head node}//print loop linked list void Printlist (node *head) {Node *temp = head;if (head! = NULL) {do{std::cout<< "" <<temp-> data<< ""; temp = temp->next;} while (temp! = head);}} int main () {const int arrsize = 6;int Arr[arrsize] = {12, 56, 2, 11, 1, 90}; Node *head = null;for (int i = 0; i < arrsize; i++) {node *newnode = new Node;newnode->data = Arr[i];sortedinsert (&am P;head, NewNode);} Printlist (head); Std::cout << Std::endl;return 0;}
Output:
1 2 11 12 56 90
Time complexity: O (n), where n is the number of nodes in a given list
Code ImprovementsThe algorithm for scenario 2 in the above code can also be optimized. You can use the method of value exchange to avoid traversing the entire list. The optimized code looks like this:The code for scenario 2 implements the else if (current->data >= newnode->data) { swap (& (Current->data), & (newnode-> data)); Suppose there is a swap function Newnode->next = (*head)->next; (*head)->next = NewNode;}
Circular link List (2)-Insert node to sorted list