From 6c660d0a11de11d59c12fb45bffaa45cb0237a21 Mon Sep 17 00:00:00 2001 From: Himanshu Rathod Date: Sat, 3 Oct 2020 13:08:21 +0530 Subject: [PATCH] Sorted Insertion Program Given a sorted linked list and a value to insert, write a function to insert the value in a sorted way. --- Linked Lists/Single LL/sortedInsertion.cpp | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Linked Lists/Single LL/sortedInsertion.cpp diff --git a/Linked Lists/Single LL/sortedInsertion.cpp b/Linked Lists/Single LL/sortedInsertion.cpp new file mode 100644 index 0000000..557fa93 --- /dev/null +++ b/Linked Lists/Single LL/sortedInsertion.cpp @@ -0,0 +1,92 @@ +/* Program to insert in a sorted list */ +#include +using namespace std; + +/* Link list node */ +class Node { +public: + int data; + Node* next; +}; + +/* function to insert a new_node +in a list. Note that this +function expects a pointer to +head_ref as this can modify the +head of the input linked list +(similar to push())*/ +void sortedInsert(Node** head_ref, + Node* new_node) +{ + Node* current; + /* Special case for the head end */ + if (*head_ref == NULL + || (*head_ref)->data + >= new_node->data) { + new_node->next = *head_ref; + *head_ref = new_node; + } + else { + /* Locate the node before the +point of insertion */ + current = *head_ref; + while (current->next != NULL +&& current->next->data +< new_node->data) { + current = current->next; + } + new_node->next = current->next; + current->next = new_node; + } +} + +/* BELOW FUNCTIONS ARE JUST +UTILITY TO TEST sortedInsert */ + +/* A utility function to +create a new node */ +Node* newNode(int new_data) +{ + /* allocate node */ + Node* new_node = new Node(); + + /* put in the data */ + new_node->data = new_data; + new_node->next = NULL; + + return new_node; +} + +/* Function to print linked list */ +void printList(Node* head) +{ + Node* temp = head; + while (temp != NULL) { + cout << temp->data << " "; + temp = temp->next; + } +} + +/* Driver program to test count function*/ +int main() +{ + /* Start with the empty list */ + Node* head = NULL; + Node* new_node = newNode(5); + sortedInsert(&head, new_node); + new_node = newNode(10); + sortedInsert(&head, new_node); + new_node = newNode(7); + sortedInsert(&head, new_node); + new_node = newNode(3); + sortedInsert(&head, new_node); + new_node = newNode(1); + sortedInsert(&head, new_node); + new_node = newNode(9); + sortedInsert(&head, new_node); + cout << "Created Linked List\n"; + printList(head); + + return 0; +} +// This is code is contributed by rathbhupendra