WRITE A C++ PROGRAM TO IMPLEMENTATION OF QUEUE USING SLL

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class NODE
{
            public:
                        int data;
                        NODE *next;
                        void insert();
                        void delet();
                        void display();
                        int isempty();
};

NODE *temp,*nw,*t1,*front=NULL,*rear=NULL;

void NODE::insert()
{
            nw=new(NODE);
            cout<<"\nEnter the data";
            cin>>nw->data;

            if(front==NULL && rear==NULL)
            {
                        front=nw;
                        rear=nw;
                        rear->next=NULL;
            }
            else
            {
                        rear->next=nw;
                        rear=rear->next;
                        rear->next=NULL;
            }
}

void NODE::display()
{
            if(isempty())
            {
                        cout<<"\nQueue is empty" ;
            }
            else
            {
                        cout<<"\nElements in queue are:";
                        temp=front;
                        while(temp!=NULL)
                        {
                                    cout<<"\n"<<temp->data;
                                    temp=temp->next;
                        }
            }
}

void NODE::delet()
{
            if(isempty())
            {
                        cout<<"\nQueue is empty";
            }
            else
            {
                        if(front==rear)
                        {
                                    cout<<"\nDeleted element is"<<front->data;
                                    front=NULL;
                                    rear=NULL;
                        }
                        else
                        {
                                    t1=front;
                                    front=front->next;
                                    cout<<"\nDeleted element is:"<<t1->data;
                                    free(t1);
                        }
            }
}
int NODE::isempty()
{
            if(front==NULL && rear==NULL)
            {
                        return 1;
            }
            else
                        return 0;
}
void main()
{
            NODE d;
            int ch;
           
            do
            {
                        cout<<"\n1.Insert Element\n2.Delete Element\n3.Display elements\n4.exit";
                        cout<<"\n Enter your choice";
                        cin>>ch;
                        switch(ch)
                        {
                                    case 1:
                                                cout<<"\n***Insert Element***";
                                                d.insert();
                                                break;
                                    case 2:
                                                cout<<"\n***Delete Element***";
                                                d.delet();
                                                break;
                                    case 3:
                                                cout<<"\n***Display Elements***";
                                                d.display();
                                                break;
                                    case 4:
                                                exit(0);
                                                break;
                                    default :
                                                 cout<<"invalid choice";
                                                 break;
                        }
            }while(ch!=5);
            getch();
}

-----------------------------------------/*OUTPUT*/---------------------------------------------

1.Insert Element
2.Delete Element
3.Display elements
4.exit
 Enter your choice1

***Insert Element***
Enter the data
12

1.Insert Element
2.Delete Element
3.Display elements
4.exit
 Enter your choice1

***Insert Element***
Enter the data2

1.Insert Element
2.Delete Element
3.Display elements
4.exit
 Enter your choice1

***Insert Element***
Enter the data25

1.Insert Element
2.Delete Element
3.Display elements
4.exit
 Enter your choice1

***Insert Element***
Enter the data87

1.Insert Element
2.Delete Element
3.Display elements
4.exit
 Enter your choice3

***Display Elements***
Elements in queue are:
12
2
25
87
1.Insert Element
2.Delete Element
3.Display elements
4.exit
 Enter your choice2

***Delete Element***
Deleted element is:12
1.Insert Element
2.Delete Element
3.Display elements
4.exit
 Enter your choice3

***Display Elements***
Elements in queue are:
2
25
87
1.Insert Element
2.Delete Element
3.Display elements
4.exit

 Enter your choice
Previous
Next Post »

Disqus Shortname

Comments system