C++ program for insertion in a Sorted Linked List

C++ program for insertion in a sorted linked list

How to insert data in a sorted linked list?

In the following article, we will learn to write a C++ program for insertion in a sorted linked list with a condition that list input by user is already sorted. If the list is unsorted then first we need to sort the linked list and later we can perform the steps and algorithm mentioned below. Insertion in a linked can simple be perform in 3 different manners -:

  • Insertion at Beginning
  • Insertion at end
  • Insertion at nth position

This article is almost similar as to insertion at nth position, we will be using the same logic the only main difference is that we will be working on a sorted linked list, so the position of the element will be decided after comparing it with all the smaller elements of the linked list.

Steps to write a C++ program for insertion in a sorted linked list

To insert a node in a sorted linked list in C++ following steps are followed

  1. Define the linked list.
  2. In the main method call all the method you have declared with input/output statement you want to use.
  3. Function named build is used to build the linked list.
  4. Print will be used to display the list.
  5. newNode will construct new node for insertion.
  6. Now in our function named sortedInsert core process of the program take place.
  7. First if the list is empty or data at previous head is greater then the data we are inserting, we will insert new node as head in the list.
  8. Else we will traverse through the linked list and insert new node at the required position.

How to construct a linked list ?

class Node  

    int data; 
    Node *next; 
}; 
C++ program for insertion in a sorted linked list

Algorithm for insertion in a sorted linked list in C++

  1. IF (*HEAD==NULL || (*HEAD)->DATA>=NEWNODE->DATA
  2. NEWNODE->NEXT=*HEAD
  3. *HEAD=NEWNODE
  4. RETURN AND END IF
  5. WHILE(CURRENT->NEXT!=NULL && CURRENT->NEXT->DATADATA)
  6. CURRENT=CURRENT->NEXT
  7. NEWNODE->NEXT=CURRENT->NEXT
  8. CURRENT->NEXT=NEWNODE

Program for insertion in a sorted linked list in C++

Run
#include<iostream>
using namespace std;

struct node
{
  int data;
  struct node *next;
};
void build (struct node **head, int data);
void display (struct node *head);
void sortedInsert (struct node **head, struct node *newNode);
struct node *newNode (int data);

int main (void)			//main method.
{
  int n, k;
  cout << "Enter size of linked list\n"; cin >> n;
  int data[100];
  cout << "Enter linked list data in sorted order\n";

  struct node *head = NULL;

  cout << "Linked list before insertion\nLinked List Data: ";
  display (head);
  for (int i = 0; i < n; i++)
    {
      cout << "\nEnter data you want to insert: "; cin >> k;
      sortedInsert (&head, newNode (k));
    }
  cout << "\nLinked list after insertion\nLinked List Data: "; display (head); return 0; } void build (struct node **head, int data) //function to build linked list { struct node *newNode = (struct node *) malloc (sizeof (struct node)); newNode->data = data;
  newNode->next = *head;
  *head = newNode;
}

void display (struct node *head)	//function to print linked list
{
  struct node *ptr = head;
  while (ptr)
    {
      cout << ptr->data << " "; ptr = ptr->next;
    }
}

struct node *newNode (int data)
{
  struct node *newNode = (struct node *) malloc (sizeof (struct node));
  newNode->data = data;
  newNode->next = NULL;
  return newNode;
}

void sortedInsert (struct node **head, struct node *newNode)
//function to insert data in sorted position
{
  //If linked list is empty
  if (*head == NULL || (*head)->data >= newNode->data)
    {
      newNode->next = *head;
      *head = newNode;
      return;
    }

  //Locate the node before insertion
  struct node *current = *head;
  while (current->next != NULL && current->next->data < newNode->data)
    current = current->next;

  newNode->next = current->next;
  current->next = newNode;
}

Output

Enter size of linked list
4
Enter linked list data in sorted order
1
2
4
5
Linked list before insertion
Linked List Data: 1 2 4 5
Enter data you want to insert: 3

Linked list after insertion
Linked List Data: 1 2 3 4 5

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java