Singly Linked list with all operations in C++

Linked list with all operations. Insert, Delete items at various position.

#include <iostream.h>
#include <conio.h>
//singly link list
class linklist
{
    struct node
    {
        int info;
        node *next;
    }*list;
    public:
        linklist(){list=NULL;}
        void insatbeg();
        void insatend(int data);
        void insafter(int t);
        void insbefore(int t,int p);
        void delatbeg();
        void delatend();
        void delafter(int t);
        int delbefore(int t);
        void move(int s,int d);
        void display();
        ~linklist();
};

void linklist::insatbeg()
{
    node *newnode;
    newnode=new node;
    cout<<"Enter the data:"<<endl;
    cin>>newnode->info;
    newnode->next=list;
    list=newnode;
}

void linklist::insatend(int data)
{
    node *newnode,*last=list,*target=list;
    newnode=new node;
    if(data==0)
    {   cout<<"Enter the data:"<<endl;
        cin>>newnode->info;
    }
    else newnode->info=data;
    int a=0;
    while(target!=NULL)
    {
        a++;
        target=target->next;
    }
    if(a==0)
    {
        list=newnode;
        newnode->next=NULL;
    }
    else
    {
        newnode->next=NULL;
        for(int i=1;i<a;i++)
            last=last->next;
        last->next=newnode;
    }
}

void linklist::insafter(int t)
{
    node *newnode,*target=list;
    int a=0;
    while(target!=NULL)
    {
        a++;
        if(a==t) break;
        target=target->next;
    }
    if(a!=t || a==0) cout<<"Node not present"<<endl;
    else
    {
        newnode=new node;
        cout<<"Enter the data:"<<endl;
        cin>>newnode->info;
        newnode->next=target->next;
        target->next=newnode;
    }
    getch();
}

void linklist::insbefore(int t,int p)
{
    node *newnode,*target=list,*prev=list;
    int a=0;
    while(target!=NULL)
    {
        a++;
        if(a==t) break;
        target=target->next;
    }
    if(a!=t || a==0) cout<<"Node not present"<<endl;
    else
    {
        newnode=new node;
        if(p==0)
        {
            cout<<"Enter the data:"<<endl;
            cin>>newnode->info;
        }
        else
        {
            newnode->info=p;
        }
        if(a==1)
        {
            newnode->next=list;
            list=newnode;
        }
        else
        {
            for(int i=1;i<(a-1);i++)
                prev=prev->next;
            newnode->next=prev->next;
            prev->next=newnode;
        }
    }
    getch();
}

void linklist::delatbeg()
{
    node *target=list;
    int a=0;
    while(target!=NULL)
    {
        a++;
        target=target->next;
    }
    if(a==0) cout<<"The list is empty"<<endl;
    else
    {
        target=list;
        list=list->next;
        delete target;
    }
    getch();
}

void linklist::delatend()
{
    node *prev=list,*target=list;
    int a=0;
    while(target!=NULL)
    {
        a++;
        target=target->next;
    }
    if(a==0) cout<<"The list is empty"<<endl;
    else if(a==1)
    {
        target=prev->next;
        list=NULL;
    }
    else
    {
        for(int i=1;i<(a-1);i++)
            prev=prev->next;
        target=prev->next;
        prev->next=NULL;
    }
    delete target;
    getch();
}

void linklist::delafter(int t)
{
    node *target=list,*prev=list;
    int a=0,x=0;
    while(target!=NULL)
    {
        x++;
        target=target->next;
    }
    target=list;
    while(target!=NULL)
    {
        a++;
        if(a==t) break;
        target=target->next;
    }
    if(a!=t || a==0) cout<<"Node not present"<<endl;
    else if(a==x && x==t) cout<<"The target is last node,so no node to bedeleted"<<endl;
    else
    {
        for(int i=1;i<a;i++)
            prev=prev->next;
        target=prev->next;
        prev->next=target->next;
        delete target;
    }
    getch();
}

int linklist::delbefore(int t)
{
    node *target=list,*prev=list,*current=list;
    int a=0,data;
    while(current!=NULL)
    {
        a++;
        if(a==t) break;
        current=current->next;
    }
    if(a!=t || a==0 ) cout<<"Node not present"<<endl;
    else if(a==1) cout<<"No node to be deleted before first node"<<endl;
    else if(a==2)
    {
        target=list;
        list=target->next;
        data=target->info;
        delete target;
    }
    else
    {
        for(int i=1;i<(a-2);i++)
            prev=prev->next;
        target=prev->next;
        prev->next=target->next;
        data=target->info;
        delete target;
    }
    getch();
    return data;
}

void linklist::move(int s,int d)
{
    int a=0,b=0,c=0;
    node *temp=list;
    while(temp!=NULL)
    {
        c++;
        temp=temp->next;
    }
    temp=list;
    while(temp!=NULL)
    {
        a++;
        if(a==s) break;
        temp=temp->next;
    }
    temp=list;
    while(temp!=NULL)
    {
        b++;
        if(b==d) break;
        temp=temp->next;
    }
    if(a!=s || a==0 || b!=d || b==0) cout<<"Node not present";
    else
    {
        int data=delbefore(s+1);
        if(c!=d) insbefore(d,data);
        else insatend(data);
    }
    getch();
}

void linklist::display()
{
    int a=0;
    clrscr();
    node *target=list;
    cout<<"The datas in linklist are"<<endl;
    while(target!=NULL)
    {
        a++;
        cout<<"\t"<<a<<"."<<target->info<<endl;
        target=target->next;
    }
    if(a==0)
    {
        clrscr();
        cout<<"List is empty";
    }
    getch();
}

linklist::~linklist()
{
    node *q ;
    while (list!= NULL)
    {
        q=list->next ;
        delete list ;
        list=;
    }
}

void main()
{
    linklist l;
    int ch,ch1,ch2,t;
    char c;
    do
    {
        clrscr();
        cout<<"Menu"<<endl;
        cout<<"1.Insert a node"<<endl;
        cout<<"2.Delete a node"<<endl;
        cout<<"3.Move a node"<<endl;
        cout<<"4.Display all data"<<endl;
        cout<<"5.Exit"<<endl;
        cout<<"Enter your choice"<<endl;
        cin>>ch;
        switch(ch)
        {
        //insert at various position of list
            case 1:
                clrscr();
                cout<<"Insert a node"<<endl;
                cout<<"1.At the beginning"<<endl;
                cout<<"2.At the end"<<endl;
                cout<<"3.After a node"<<endl;
                cout<<"4.Before a node"<<endl;
                cout<<"Enter your choice"<<endl;
                cin>>ch1;
                switch(ch1)
                {
                    case 1:
                        l.insatbeg();
                        break;
                    case 2:
                        l.insatend(0);
                        break;
                    case 3:
                        cout<<"Enter the node after which datais to be inserted:"<<endl;
                        cin>>t;
                        l.insafter(t);
                        break;
                    case 4:
                        cout<<"Enter the node after which datais to be inserted:"<<endl;
                        cin>>t;
                        l.insbefore(t,0);
                        break;
                    default:
                        break;
                }
                break;
        //delete at various position of list               
        case 2:
                clrscr();
                cout<<"Delete a node"<<endl;
                cout<<"1.At the beginning"<<endl;
                cout<<"2.At the end"<<endl;
                cout<<"3.After a node"<<endl;
                cout<<"4.Before a node"<<endl;
                cout<<"Enter your choice"<<endl;
                cin>>ch2;
                switch(ch2)
                {
                    case 1:
                        l.delatbeg();
                        break;
                    case 2:
                        l.delatend();
                        break;
                    case 3:
                        cout<<"Enter the node after which datais to be deleted:"<<endl;
                        cin>>t;
                        l.delafter(t);
                        break;
                    case 4:
                        cout<<"Enter the node after which datais to be deleted:"<<endl;
                        cin>>t;
                        l.delbefore(t);
                        break;
                    default:
                        break;
                }
                break;
            case 3:
                clrscr();
                int s,d;
                cout<<"To move data from source todestination"<<endl;
                cout<<"Enter the source node"<<endl;
                cin>>s;
                cout<<"Enter the destinationnode"<<endl;
                cin>>d;
                l.move(s,d);
                break;
            case 4:
                l.display();
                break;
            case 5:
                break;
            default:
                cout<<"Out of Menu"<<endl;
                getch();
                break;
        }
    }while(ch!=5);
}

No comments :

Post a Comment

Your Comment and Question will help to make this blog better...