Friday, October 7, 2016

Linked list implementation code in c++

#include<bits/stdc++.h>
using namespace std;

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

struct head
{
    int count;
    struct node *p;
}*head;

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

void create_head()
{
    head=(struct head*)malloc(sizeof(struct head));
    head->p=NULL;
    head->count=0;
}

void insert(int data)
{
    create_node(data);

    int n,n1;
    cout<<"1.1st position\n2.last position\n3.nth position\n";
    cout<<"Enter your choice : ";
    cin>>n;

    switch(n)
    {
    case 1:

        node->next=head->p;
        head->p=node;
        break;

    case 2:

        struct node *temp;
        temp=head->p;

        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        temp->next=node;
        break;

    case 3:

        cout<<"Enter position to insert : ";
        cin>>n1;

        struct node *temp1;
        temp1=head->p;

        while(n1>2)
        {
            temp1=temp1->next;
            n1--;
        }

        node->next=temp1->next;
        temp1->next=node;
        break;
    }

    head->count++;
}

void print()
{
    struct node *temp;

    temp=head->p;

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

    cout<<endl<<"Total number of data is : "<<head->count<<endl<<endl;
}

void insert_anywhere(int data)
{
    int data1,n;

    cout<<"Enter a target data : ";
    cin>>data1;
    cout<<"1.Before\n2.After\n";
    cout<<"Enter your choice : ";
    cin>>n;

    create_node(data);

    switch(n)
    {
    case 1:
        int s;
        s=0;
        struct node *temp;
        temp=head->p;

        while(temp->next->data!=data1)
        {
            temp=temp->next;
        }

        node->next=temp->next;
        temp->next=node;

        break;

    case 2:
        temp=head->p;

        while(temp->next!=NULL && temp->data!=data1)
        {
            temp=temp->next;
        }

        node->next=temp->next;
        temp->next=node;

        break;
    }

    head->count++;
}

bool search(int data)
{
    struct node *temp;
    int s=0;
    temp=head->p;

    while(temp!=NULL)
    {
        if(temp->data==data)
        {
            s=1;
            break;
        }
        temp=temp->next;
    }

    if(s==0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

void vanish_or_destroy()
{
    free(head->p);
    head->p=NULL;
    head->count=0;

    print();
}

void delete1()
{
    int n;
    cout<<"1.delete 1st element\n2.delete last element\n3.delete nth element"<<endl;
    cout<<"Enter your choice : ";
    cin>>n;

    int c,n1;


    switch(n)
    {
    case 1:
         struct node *temp;
         temp=head->p;
         head->p=temp->next;
         free(temp);
        break;

    case 2:

         temp=head->p;

         while(temp->next->next!=NULL)
         {
             temp=temp->next;
         }
        free(temp->next);
        temp->next=NULL;

        break;

    case 3:

        cout<<"Enter the position : ";
        cin>>n1;

        temp=head->p;
        c=1;

        while(c<(n1-1))
        {
            temp=temp->next;
            c++;
        }

        struct node *temp1;
        temp1=temp->next;
        temp->next=temp1->next;
        free(temp1);

        }

        head->count--;

}

void delete_anywhere()
{
    int n;

    cout<<"Enter a number to delete : ";
    cin>>n;

    struct node *temp;

    if(search(n)==true)
    {
        temp=head->p;

        while(temp->next->data!=n)
        {
            temp=temp->next;
        }

        struct node *temp1;

        temp1=temp->next;
        temp->next=temp1->next;
        free(temp1);
    }
    else
    {
        cout<<"Data isn't exist"<<endl;
    }
    head->count--;
}

int main()
{
    int n;

    create_head();

    while(1)
    {
        cout<<"----->>>>LINKED LIST OPERATIONS<<<<-----"<<endl;
    cout<<"\t1.INSERT\n\t2.DELETE\n\t3.SEARCH\n\t4.PRINT\n\t5.VANISH\n\t6.INSERT ANYWHERE\n\t7.DELETE ANYWHERE\n\t8.EXIT\n\n";
    cout<<"ENTER YOUR CHOICE : ";
    cin>>n;

    switch(n)
    {
    case 1:
        int data;
        cout<<"ENTER A DATA TO INSERT : ";
        cin>>data;

        insert(data);
        break;

    case 2:
        delete1();
        break;

    case 3:
        cout<<"ENTER A NUMBER TO SEARCH : ";
        cin>>data;
        if(search(data)==true)
        {
          cout<<"FOUND"<<endl;
        }
        else
        {
            cout<<"NOT FOUND"<<endl;
        }
        break;

    case 4:

        print();
        break;

    case 5:
        vanish_or_destroy();
        break;

    case 6:
        cout<<"ENTER A DATA TO INSERT : ";
        cin>>data;
        insert_anywhere(data);
        break;

    case 7:
        delete_anywhere();
        break;

    case 8:
        exit(0);
    }

    }
    return 0;
}

No comments:

Post a Comment