0% found this document useful (0 votes)
0 views

QueueUsingArray

The document contains a C++ program that implements a Queue using an array with a maximum size of 100. It includes methods for enqueueing, dequeueing, and displaying the queue contents, along with a simple menu for user interaction. The program allows users to insert, remove, and view items in the queue until they choose to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

QueueUsingArray

The document contains a C++ program that implements a Queue using an array with a maximum size of 100. It includes methods for enqueueing, dequeueing, and displaying the queue contents, along with a simple menu for user interaction. The program allows users to insert, remove, and view items in the queue until they choose to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8. Write a program to implement Queue using Array.

#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 100

class Queue
{
private:
int item, i;
int queue[MAX_SIZE];
int rear;
int front;
public:
Queue() {
rear = 0;
front = 0;
}

void enQueue()
{
if (rear == MAX_SIZE)
cout << "\n Queue Reached Max!";
else
{
cout << "\nEnter The Value to be Insert : ";
cin>>item;
cout << "\n## Position : " << rear + 1 << " , Insert Value : " << item;
queue[rear++] = item;
}
}

void deQueue()
{
if (front == rear)
cout << "\n## Queue is Empty!";
else
{
cout << "\n## Position : " << front << " , Remove Value :" << queue[front];
front++;
}
}

void display()
{
cout << "\n## Queue Size : " << (rear - front);
for (i = front; i < rear; i++)
cout << "\n## Position : " << i << " , Value : " << queue[i];
}
};

int main()
{
int choice, exit_p = 1;
Queue q;
do
{
cout << "\n\n Queue Main Menu";
cout << "\n1.enQueue \n2.deQueue \n3.Display \n Others to exit";
cout << "\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
q.enQueue();
break;
case 2:
q.deQueue();
break;
case 3:
q.display();
break;
default:
exit_p = 0;
break;
}
} while (exit_p);

return 0;
}

Output:
Queue Main Menu 1.enQueue 2.deQueue 3.Display Others to exit
Enter Your Choice : 1
Enter The Value to be Insert : 1
## Position : 1 , Insert Value : 1

Queue Main Menu 1.enQueue 2.deQueue 3.Display Others to exit


Enter Your Choice : 1
Enter The Value to be Insert : 2
## Position : 2 , Insert Value : 2

Queue Main Menu 1.enQueue 2.deQueue 3.Display Others to exit


Enter Your Choice : 1
Enter The Value to be Insert : 3
## Position : 3 , Insert Value : 3
Queue Main Menu 1.enQueue 2.deQueue 3.Display Others to exit
Enter Your Choice : 2
## Position : 0 , Remove Value :1

Queue Main Menu 1.enQueue 2.deQueue 3.Display Others to exit


Enter Your Choice : 3
## Queue Size : 2
## Position : 1 , Value : 2
## Position : 2 , Value : 3

You might also like