Queue using array
#include <iostream>
using namespace std;
void enQueue();
void deQueue();
void display();
#define SIZE 10
int queue[SIZE];
int front = -1, rear = -1;
void enQueue()
{
int value;
cout << "enter the value to insert: ";
cin >> value;
if (rear == SIZE - 1)
cout << "\nOverflow. Queue is Full.";
else
{
if (front == -1)
front = 0;
rear++;
queue[rear] = value;
cout << "\nInsertion was successful";
}
}
void deQueue()
{
if (front == rear)
cout << "\nUnderflow. Queue is Empty.";
else
{
cout << "\nDeleted item is:" << queue[front];
front++;
if (front == rear)
front = rear = -1;
}
}
void display()
{
int i;
if (front == rear) // reasr ==-1
{
cout << "\nQueue is Empty\n";
return;
}
// traverse front to rear and print elements
for (i = front; i <= rear; i++)
{
cout << " <-- " << queue[i];
}
return;
}
int main()
{
int choice;
while (1)
{
cout << "1.enqueue\n2.dequeue\n3.display\n4Exit" << endl;
cout << "Enter the choice";
cin >> choice;
switch (choice)
{
case 1:
enQueue();
break;
case 2:
deQueue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout << "invalid";
}
}
return 0;
}