Write a menu driven program to create a linked list and to perform insert and delete operations


#include
#include
#include
#include
#include

int insbeg(int item);
int inspos(int item);
int insend(int item);
int delbeg();
int delpos();
int delend();

struct node
{
            int info;
            struct node *link;
};

typedef struct node n1;
n1 *start = NULL;
n1 *currptr, *newnode, *preptr;

class list
{
            public:
                        int create();
                        int display();
                        int ins();
                        int del();
};

int list::create()
{
            char ch;
            start = new n1;
            currptr = start;
            while (1)
            {
                        cout << "\nEnter the item value :\t" ;
                        cin >> currptr->info;
                        cout << "\nContinue? (y/n) : " ;
                        flushall();
                        cin >> ch;
                        if (ch=='y')
                        {
                                    newnode = new n1;
                                    currptr->link = newnode;
                                    currptr = newnode;
                        }
                        else
                        {
                                    currptr->link = NULL;
                                    break;
                        }
            }
            return 0;
}

int list::display()
{
            if (start==NULL)
            {
                        cout << "\nEmpty list" ;
                        exit(0);
            }
            else
            {
                        currptr = start;
            }
            cout << "\nThe list is : \n" ;
            while (currptr->link!=NULL)
            {
                        cout << currptr->info;
                        cout << "--->" ;
                        currptr = currptr->link;
            }
            cout << currptr->info;
            cout << "--->" ;
            cout << "NULL" ;
            return 0;
}

int list::ins()
{
            int ch, item;
            cout << "\nEnter the item to insert :\t" ;
            cin >> item;
            newnode = new n1;
            cout << "\n\nChoose any given option : ";
            cout << "\n\t 1. At the beginning" ;
            cout << "\n\t 2. At a position" ;
            cout << "\n\t 3. At the end";
            cout << "\n\nEnter your choice :\t" ;
            fflush(stdin);
            cin >> ch;
            switch (ch)
            {
            case 1: insbeg(item);
                                                  break;
            case 2: inspos(item);
                                                  break;
            case 3: insend(item);
                                                  break;
            default: cout << "\nInvalid choice" ;
            }
            return 0;
}

insbeg(int item)
{
            newnode->info = item;
            newnode->link = start;
            start = newnode;
            return 0;
}

inspos(int item)
{
            int i, pos;
            currptr = start;
            cout << "\nEnter the position :\t" ;
            cin >> pos;
            if (pos==1)
            {
                        insbeg(item);
            }
            else
            {
                        for (i=1 ;i
                        {
                                    currptr = currptr->link;
                        }
                        newnode->link = currptr->link;
                        newnode->info = item;
                        currptr->link = newnode;
            }
            return 0;
}

insend(int item)
{
            currptr =start;
            if (start->link==NULL)
            {
                        currptr->link = newnode;
                        newnode->info = item;
                        newnode->link = NULL;
            }
            else
            {
                        while (currptr->link!=NULL)
                        {
                                    currptr = currptr->link;
                        }
                        currptr->link = newnode;
                        newnode->info = item;
                        newnode->link = NULL;
            }
            return 0;
}

int list::del()
{
            int cho;
            cout << "\n\nChoose any given option : ";
            cout << "\n\t 1. Delete from beginning" ;
            cout << "\n\t 2. Delete from position" ;
            cout << "\n\t 3. Delete from end";
            cout << "\n\nEnter your choice :\t" ;
            fflush(stdin);
            cin >> cho;
            switch (cho)
            {
            case 1: delbeg();
                                                  break;
            case 2: delpos();
                                                  break;
            case 3: delend();
                                                  break;
            default: cout << "\nInvalid choice" ;
            }
            return 0;
}

delbeg()
{
            currptr =start;
            start = currptr->link;
            delete currptr;
            return 0;
}

delpos()
{
            int i, pos;
            cout << "\nEnter the position :\t" ;
            cin >> pos;
            if (pos==1)
            {
                        delbeg();
            }
            else
            {
                        currptr = start;
                        preptr = NULL;
                        for (i=1 ;i
                        {
                                    preptr = currptr;
                                    currptr = currptr->link;
                        }
                        preptr->link = currptr->link;
                        delete currptr;
            }
            return 0;
}

delend()
{
            currptr =start;
            if (start->link==NULL)
            {
                        start = NULL;
                        delete currptr;
            }
            else
            {
                        preptr = NULL;
                        while (currptr->link!=NULL)
                        {
                                    preptr = currptr;
                                    currptr = currptr->link;
                        }
                        preptr->link = NULL;
                        delete currptr;
            }
            return 0;
}

void main()
{
            list obj;
            int ch1;
            char ch2;
            clrscr();
            cout << "\nCreate a linked list" ;
            obj.create();
            do
            {
                        cout << "\n\nChoose any given option : " ;
                        cout << "\n\t 1. Insertion" ;
                        cout << "\n\t 2. Deletion" ;
                        cout << "\n\nEnter your choice :\t" ;
                        cin >> ch1;
                        switch (ch1)
                        {
                        case 1: obj.ins();
                                                              break;
                        case 2: obj.del();
                                                              break;
                        default: cout << "\n\nInvalid choice" ;
                        }
                        cout << "\n\nDo you want to continue? (y/n) :\t" ;
                        fflush(stdin);
                        cin >> ch2;
            }
            while (ch2=='y');
            obj.display();
            getch();

}

Comments

Popular posts from this blog

To convert hexadecimal to decimal numbers.

To convert hexadecimal to decimal numbers.