Tuesday 27 November 2018

Singly Linklist in C++ (insertion at satart,middle and end. Searching in linklist. Deletion of any node)


#include <iostream>

using namespace std;

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

int main()

{
    node *head;
    node *temp;
    node *last;

    last=0;

    int choice;

   do

    {
        temp=new node();
        cout<<"Enter values in a node:"<<endl;
        cin>>temp->data;
        if(last==0)
        {
            head=last=temp;

        }

        else
        {
            last->next=temp;
            last=temp;
        }
        cout<<"Enter your choice:"<<endl;
        cin>>choice;
    }while(choice==1);

        last->next=0;

        temp=head;

        while(temp!=0)

        {
            cout<<temp->data<<" ";
            temp=temp->next;
        }
        /************insertion at the start of the node***************************/
        do{
        temp=new node();
        cout<<"Enter value at the start of the node: "<<endl;
        cin>>temp->data;

        temp->next=head;

        head=temp;

        cout<<"Enter your choice: "<<endl;

        cin>>choice;
        }while(choice==1);

        temp=head;

        while(temp!=0)
        {
            cout<<temp->data<<" ";
            temp=temp->next;

        }


        /************** iNSERTION AT MIDDLE OF THE LINKLIST***********/

        node *temp2;
        int location=0;

        cout<<"\n Enter your location"<<endl;

        cin>>location;

        temp=new node();

        cout<<"\n Enter data in node: "<<endl;
        cin>>temp->data;

        temp2=head;


        while(location-1>0)

        {
            location--;
            temp2=temp2->next;
        }
        temp->next=temp2->next;
        temp2->next=temp;

        temp=head;

        while(temp!=0)
        {
            cout<<temp->data<<" ";
            temp=temp->next;
        }

    /**************************Search traverse*************/

    int value;
    int count=1;
    int flag=0;
    cout<<"\n Enter value you want to search in LinkList: "<<endl;
    cin>>value;
    temp=head;
    while(temp!=0)
    {
        if(temp->data==value)
        {
            cout<<"Data found successfully at index "<<count<<endl;
            flag=1;
            break;
        }
        else
        {
            temp=temp->next;
            count++;
        }
    }

    if(flag==0)

    {
        cout<<"Data not found"<<endl;
    }
    /**************************deleting node******************/
    int b=0;
    int item;
    node *temp3;
    cout<<"which data do you want to delete?"<<endl;
    cin>>item;
    temp=head;
    while(temp->next->next!=0)
    {
        if(temp->next->data==item)
        {
            cout<<"Data found successfully"<<endl;
            b=1;
            break;
        }
        else
        {
            temp=temp->next;
            temp3=temp->next;
        }
    }

    if(b==0)

    {
        cout<<"Data not found"<<endl;
    }
    else
    {
        temp->next=temp->next->next;
        delete temp3;
    }

    temp=head;

        while(temp!=0)
        {
            cout<<temp->data<<" ";
            temp=temp->next;
        }


    return 0;

}

0 comments:

Post a Comment