#include<iostream>

using namespace std;

class node

{

public :

    int d; //data

    node *p; // contains the address of the next node

};

class linked

{

public :

    node *start;

public :

    linked()

    {

        start=NULL;

    }

    void insert_at_beg();

    void insert_at_mid();

    void insert_at_end();

    int remove();

    void display();

};

void linked :: insert_at_end()

{

    node *temp;

    if(start==NULL)

    {

        start=new node [1];

        start->p=NULL;

        cout<<"\n Enter Data to insert \n";

        cin>>start->d;

}

    else

    {

        temp=start;

        while(temp->p!=NULL)

            temp=temp->p;

        temp->p= new node [1];

        temp=temp->p;

        cout<<"\n Enter Data to insert \n";

        cin>>temp->d;

        temp->p=NULL;

    }

}

void linked :: insert_at_mid()

{

    node *temp,*next;

    int x;

temp=start;

    cout<<"\n Enter the Neighbour element \n";

    cin>>x;

    while(temp->d!=x)

        temp=temp->p;

    next=temp->p;

    temp->p=new node [1];

    temp=temp->p;

    temp->p=next;

    cout<<"\n Enter data to insert \n";

    cin>>temp->d;

}

void linked :: insert_at_beg()

{

    node *temp;

    temp=new node [1];

    temp->p=start;

start=temp;

    cout<<"\n Enter element to insert \n";

    cin>>temp->d;

}

int linked :: remove()

{

    node *temp,*prev;

    int x;

    if(start==NULL)

    {

        cout<<"\n Underflow -- No element to Delete \n";

        return 0;

    }

    cout<<"\n Enter element to delete \n";

    cin>>x;

    if(start->d==x && start->p==NULL)

    {

delete start;

        start=NULL;

        cout<<"\n Deletion successfull \n";

        return 0;

    }

    if(start->d==x)

    {

        temp=start->p;

        delete start;

        start=temp;

        cout<<"\n Deletion Successfull \n";

        return 0;

    }

    temp=start;

    while(temp->d!=x)

    {

        prev=temp;

        temp=temp->p;

    }

    prev->p=temp->p;

    cout<<"\n Deletion Successfull \n";

    return x;

}

void linked :: display()

{

    node *temp;

    temp=start;

    cout<<"\n Linked List elements are \n";

    while(temp->p!=NULL)

    {

        cout<<""<<temp->d<<" ";

        temp=temp->p;

    }

    cout<<temp->d<<endl;

}

int main()

{

    int n=0,a;

    linked l;

    do

    {

        cout<<"\n ************** M E N U **************\n";

        cout<<"\n1.Insert at beginning\n2.Insert at middle\n3.Insert at end\n4.Delete\n5.Display elements";

        cout<<"\n6.Exit\n";

        cout<<"Enter option \n";

        cin>>a;

        switch(a)

        {

        case 1:

            l.insert_at_beg();

            break;

        case 2:

            l.insert_at_mid();

            break;

        case 3:

            l.insert_at_end();

            break;

        case 4:

            l.remove();

            break;

        case 5:

            l.display();

            break;

        case 6:

            n=1;

            break;

        default :

            cout<<"\n Invalid Entry \n";

            break;

        }

    }while(n!=1);

    return 0;

}
Posted by Unknown On 01:57 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *