Tuesday, 23 September 2014

Queue Using Linked List

$$$ QUEUE IMPLEMENTATION USING LINKED LIST OR POINTER $$$

PROGRAM:

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct queue
{
                int data;
                struct queue *next;
}node;
node *newn,*front,*rear,*temp;
class queuep
{
public:
void enqueue()
{
                                newn=new node;
                                cout<<"Enter the element to be inserted:\t";
                                cin>>newn->data;
                                newn->next=NULL;
                                if(front==NULL)
                                {
                                                front=newn;
                                                rear=newn;
                                }
                                else
                                {
                                                rear->next=newn;
                                                rear=newn;
                                }
}
void dequeue()
{
                                if(front==NULL)
                                {
                                                cout<<"Queue is empty or underflow\n";
                                }
                                else
                                {
                                                temp=front;
                                                front=front->next;
                                                cout<<"The deleted element is:"<<temp->data<<endl;
                                }
                                delete(temp);
}
void display()
{
                                if(front==NULL)
                                {
                                                cout<<"Queue is empty or underflow\n";
                                }
                                else
                                {
                                                temp=front;
                                                cout<<"Content of the queue is:\n";
                                                while(temp!=NULL)
                                                {
                                                                cout<<temp->data<<endl;
                                                                temp=temp->next;
                                                }
                                }
}
};

void main()
{
                queuep q;
                int ch;
                cout<<"\n\n\n$$$ QUEUE IMPLEMENTATION USING LINKED LIST OR POINTER $$$\n\n\n";
                while(1)
                {
                                cout<<"QUEUE OPERATIONS ARE\n";
                                cout<<"1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n";
                                cout<<"Enter your choice:\t";
                                cin>>ch;
                                switch(ch)
                                {
                                                case 1:q.enqueue();break;
                                                case 2:q.dequeue();break;
                                                case 3:q.display();break;
                                                case 4:exit(0);
                                                default:cout<<"Enter the correct option\n";
                                                break;
                                }
                }
}


OUTPUT:

$$$ QUEUE IMPLEMENTATION USING LINKED LIST OR POINTER $$$

 QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be inserted:       11

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be inserted:       22

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be inserted:       33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      3
Content of the queue is:
11
22
33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      2
The deleted element is: 11

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:           3
Content of the queue is:
22
33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT

Enter your choice:      4


No comments:

Post a Comment