Answer to Question #23779 in C++ for Rohan Saha

Question #23779
Write an interactive menu driven c++ program to implement queue using array. 1.Insert an element in queue 2.Delete an element from queue. 3.Show the contents of the queue. 4.Exit
1
Expert's answer
2013-02-06T07:45:19-0500
#include <iostream>

using namespace std;

void main()
{
char choice;
int tmp;

int queue[100];
int queueLength = 0;

cout << endl;
cout << " 1. Insert an element in queue" << endl;
cout << " 2. Delete an element from queue" << endl;
cout << " 3. Show the contents of the queue" << endl;
cout << " 4. Exit" << endl << endl;

while (true)
{
cout << "Choose the menu item: ";
cin >> choice;

switch (choice)
{
case '1':
cout << "Enter and integer value: ";
cin >> tmp;

if (queueLength < 100)
{
queue[queueLength] = tmp;
queueLength++;
}
else
cout << "Queue is full." << endl;
cout << endl;
break;

case '2':
if (queueLength > 0)
{
queueLength--;
cout << "The following element has been dequeued: " << queue[0] << endl << endl;

/* Shift array values left by one position
* to dequeue the first element of the queue */
for (int c = 0; c < queueLength; c++)
queue[c] = queue[c + 1];
}
else
cout << "The queue is empty." << endl << endl;
break;

case '3':
if (queueLength == 0)
{
cout << "The queue is empty." << endl << endl;
break;
}

cout << endl;
cout << "Queue length: " << queueLength << endl;
for (int c = queueLength - 1; c >= 0; c--)
cout << "& " << queue[c] << endl;
cout << endl;
break;

case '4':
return;

default:
cout << "Wrong menu item number." << endl;
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS