C++ Program to Implement Singly Linked List 


Introduction

In our previous two articles we have seen insertion and deletion of nodes in singly linked list .

In this article we are going to implement various operations on Singly Linked List using C++.

Before moving toward the below program, I recommend to clear the concepts by reading the previous articles  ( insertion  and  deletion ). 

Below is fully working program in C++ to implement insertion of node at beginning ,at a particular position , at the end of singly linked list. It also include deletion of node at the beginning , at a particular position ,at the end of list with output. 


#C++ Program to Implement Singly Linked List


#include<iostream>

using namespace std;


struct node
{
    int data;
    node *next;
};

node *head;

node *createNode(int value);

void insertStart(int value);

void insertPosition(int value,int pos);

void insertEnd(int value);

void display();

int deleteStart();

int deleteEnd();

int deletePosition(int position);


int main()
{
    head =NULL;
    int choise ;

    int n,key;

    int location;

    cout<<"\n\t\tOperations on linked list \n";

    do
    {
        cout<<"\n\t\t\t____Menu____\n";

        cout<<"\n\t\t  1. Create a Linked List \n";

        cout<<"\t\t  2.  Insert at Start  \n";

        cout<<"\t\t  3.  Insert at End \n";

        cout<<"\t\t  4.  Insert at a Position \n";

        cout<<"\t\t  5.  Delete node at Begining \n";

        cout<<"\t\t  6.  Delete node at End \n";

        cout<<"\t\t  7.  Delete node at Position \n";

        cout<<"\t\t  8.  Display Linked List \n";

        cout<<"\t\t  9.  Exit \n";

        cout<<"\t Yours choise :  ";

        cin>>choise;

        switch(choise)
        {

                //INSERTION

            case 1:
                    cout<<"How many nodes do you want to add ?  ";

                    cin>>n;

                    for(int i=0;i<n;i++)
                    {
                        cout<<"Enter the data :";

                        cin>>key;

                        insertEnd(key);

                    }
            break;
            case 2:
                    cout<<"Enter the data :";

                        cin>>key;

                        insertStart(key);

            break;
            case 3:
                    cout<<"Enter the data :";

                    cin>>key;

                    insertEnd(key);
            break;

            case 4:
                    if(head==NULL)

                        cout<<"t\t Sorry! Linked List is Empty!\n";

                    else
                    {
                        cout<<"Enter the position : ";
                        cin>>location;

                        cout<<"Enter the data :";
                            cin>>key;

                            insertPosition(key,location);
                    }
            break;

            // DELETION    

            case 5:

                    deleteStart();

            break;

            case 6:

                    deleteEnd();
            break;

            case 7:

                    if (head==NULL)
                    {
                        cout<<"Nothing to delete ! Linked list is empty !\n";
                    }
                    else
                    {
                        cout<<"Enter the position : ";
                        cin>>location;

                        deletePosition(location);

                    }
            break;

                        //DISPLAY
            case 8:

                    display();

            break;
        }
    }while(choise<9);
}

node *createNode(int value)
{
    node *n = new node;

    n->data = value;

    n->next = NULL;

    return n;
}

void insertStart(int value)
{
    node *newNode;

    newNode = createNode(value);

    if (head==NULL)
    {
        head = newNode;
    }
    else
    {
        newNode->next = head;

        head = newNode;
    }
        cout<<"\nNode inserted succesfully at begining "<<endl;

}

void insertEnd(int value)
{
    node *newNode = createNode(value);

    node *temp = head;

     if (head==NULL)
    {
        head = newNode;
    }
    else
    {
       
        while(temp->next!=NULL)
        {
            temp = temp->next;
        }
        temp->next= newNode;
    }
   
    cout<<"Node inserted succesfully at End \n"<<endl;

}

void insertPosition(int value,int pos)
{
    node *newNode =createNode(value);

    node *prev = head;

    int i=1;

        while(prev!=NULL)
        {
            if  ((pos-1)==i)

                break;
            else
            {
                prev = prev->next;

                i++;
            }
        }
        newNode->next = prev->next;

        prev->next = newNode;

        cout<<"\nNode inserted succesfully at position :"<<pos<<endl;
   
}

int deleteStart()
{
    if  (head==NULL)
    {
        cout<<"Nothing to delete ! Linked list is Empty";
    }
    else
    {
        node *temp = head;

        int x = temp->data;

        head = head->next;

        cout<<"The deleted node is : "<<temp->data<<endl;

        delete(temp);   
    }
}



int deleteEnd()
{
    if  (head==NULL)
    {
        cout<<"Nothing to delete ! Linked list is Empty";
    }
else
    {
        node *temp = head,*prev = NULL;

        while(temp->next!=NULL)
        {

            prev = temp;

            temp = temp->next;
        }
        cout<<"The deleted node is : "<<temp->data<<endl;

        prev->next=NULL;

        delete(temp);
    }

}


int deletePosition(int position)

{
    node *temp=head,*prev=NULL;

    int i=1;
   
   if  (position==1)

        deleteStart();
       
    else
       
    {
        while(temp!=NULL)
        {
            if (position==i)
            {
                break;
            }
            else
            {
                prev=temp;

                temp=temp->next;

                i++;
            }
        }
        prev->next = temp->next;

    cout<<"The  deleted node is : "<<temp->data<<endl;

    delete(temp);

    }
}


void display()
{
    int count =0;

    node *temp = head;

    cout<<"Displaying the elements of linked list :\n";

    while(temp!=NULL)
    {
        cout<<"\t"<<temp->data<<"\n";

        temp=temp->next;

        count++;
    }
    cout<<"Node count : "<<count<<endl;
}

 

Note :  This code is successfully compiled and run under Linux distribution Ubuntu 18.04 LTS environment .  


Download the code here

Output :


    Operations on linked list

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  1


How many nodes do you want to add ?  5
Enter the data :10


Node inserted succesfully at End

Enter the data :20
Node inserted succesfully at End

Enter the data :30
Node inserted succesfully at End

Enter the data :40
Node inserted succesfully at End

Enter the data :50
Node inserted succesfully at End


            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  8
Displaying the elements of linked list :
    10
    20
    30
    40
    50
Node count : 5

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  2
Enter the data :5

Node inserted succesfully at begining

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  3
Enter the data :100
Node inserted succesfully at End


            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  8
Displaying the elements of linked list :
    5
    10
    20
    30
    40
    50
    100
Node count : 7

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  4
Enter the position : 4
Enter the data :400

Node inserted succesfully at position :4

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  8
Displaying the elements of linked list :
    5
    10
    20
    400
    30
    40
    50
    100
Node count : 8

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  5
The deleted node is : 5

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  8
Displaying the elements of linked list :
    10
    20
    400
    30
    40
    50
    100
Node count : 7

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  6
The deleted node is : 100

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  8
Displaying the elements of linked list :
    10
    20
    400
    30
    40
    50
Node count : 6

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Begining
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  7
Enter the position : 3
The  deleted node is : 400

            ____Menu____

        1.Create a Linked List
        2.Insert at Start
        3.Insert at End
        4.Insert at a Position
        5.Delete node at Beginning
        6.Delete node at End
        7.Delete node at Position
        8.Display Linked List
        9.Exit
    Yours choise :  8
Displaying the elements of linked list :
    10
    20
    30
    40
    50
Node count : 5

Conclusion

Inertion deletion and display of singly linked list is implemented successfully in C++.