0% found this document useful (0 votes)
6 views3 pages

Push and Pop

The document defines a Queue class with methods like enqueue, dequeue, empty, full and count. It implements a queue using an array with front and rear pointers. The main function contains a menu to test the queue methods.

Uploaded by

ali yousaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Push and Pop

The document defines a Queue class with methods like enqueue, dequeue, empty, full and count. It implements a queue using an array with front and rear pointers. The main function contains a menu to test the queue methods.

Uploaded by

ali yousaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

using namespace std;

class Queue{
private:
int front;
int rear;
int arr[5];

public:
Queue()
{
front=-1;
rear=-1;
for(int i=0;i<5;i++)
{
arr[0]=0;
}
}
bool empty()
{
if(front==-1 && rear==-1)
return true;
else
return false;
}
bool full()
{
if(rear==4)
return true;
else
return false;
}
void enqueue(int v)
{
if(full())
{
cout<<"Queue is full"<<endl;
return;
}
else if(empty())
{
rear=0;
front=0;
arr[rear]=v;
}
else
{
rear++;
arr[rear]=v;
}

}
dequeue()
{
int x=0;
if(empty())
{
return 0;
}
else if(front==rear)
{
x=arr[front];
front=-1;
rear=-1;
return x;
}
else
{
x=arr[front];
arr[front]=0;
front++;
return x;
}
}
int count()
{
return(rear-front+1);
}
display()
{
for(int i=0;i<=rear;i++)
{
cout<<arr[i]<<" ";
}
}
};
int main()
{
Queue obj;
int option;
do
{
cout<<"\n\nWhat operation do you want to perform:"<<endl;
cout<<"1. Enqueue()"<<endl;
cout<<"2. dequeue()"<<endl;
cout<<"3. isempty()"<<endl;
cout<<"4. isfull()"<<endl;
cout<<"5. count()"<<endl;
cout<<"6. display()"<<endl;
cout<<"7. exit"<<endl;
cout<<"8. Clear screen"<<endl;

cin>>option;
switch(option)
{
case 1:
int v;
cout<<"Enter Value"<<endl;
cin>>v;
obj.enqueue(v);
break;
case 2:
cout<<"Dequeue Value is "<<obj.dequeue()<<endl;
break;
case 3:
if(obj.empty())
cout<<"Queue is Empty"<<endl;
else
cout<<"Queue is not empty"<<endl;
break;
case 4:
if(obj.full())
cout<<"Queue is full"<<endl;
else
cout<<"Queue is not full"<<endl;
break;
case 5:
cout<<"Total elements in array is "<<obj.count()<<endl;
break;
case 6:
obj.display();
break;
case 7:
exit(0);
break;
case 8:
system("CLS");
break;
}
}
while(option!=0);
return 0;

You might also like