Implementation of Queue with Array in C++

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

class Queue
{
int data[10];
int front, rear;
public:
Queue()
{
front=-1;
rear=-1;
}
void Add();
void Remove();
void Display();
};

void Queue::Add()
{
if((rear+1)%10!=front)
{
if(rear==-1)
front=rear=0;
else
rear=(rear+1)%10;
cout<<"Enter Data: ";
cin>>data[rear];
}
else
cout<<"Queue is Full. Overflow!!!\n";
}

void Queue::Remove()
{
if(front!=-1)
{
cout<<data[front]<<" deleted...\n";
if(front==rear)
front=rear=-1;
else
front=(front+1)%10;
}
else
{
cout<<"Queue is Empty !!! Underflow...\n";
}
}

void Queue::Display()
{
int tmp=front;
if(tmp!=-1)
{
while(tmp<=rear)
{
cout<<setw(4)<<data[tmp];
tmp=(tmp+1)%10;
}
}
else
cout<<"Queue is Empty !!! Underflow...\n";
}

void menu()
{
cout<<"QUEUE OPERATIONS\n";
cout<<"================\n";
cout<<"0. Exit\n";
cout<<"1. Add\n";
cout<<"2. Delete\n";
cout<<"3. Display\n";
cout<<"Enter your choice: ";
}

void main()
{
Queue q;
int ch;
do
{
clrscr();
menu();
cin>>ch;
switch(ch)
{
case 1: q.Add(); break;
case 2: q.Remove(); break;
case 3: q.Display(); break;
}
getch();
}while(ch);
}

/*
Output:
QUEUE OPERATIONS
================
0. Exit;
1. Add
2. Delete
3. Display
Enter your choice:
*/




No comments

Post your comments

Powered by Blogger.