C++ Implement a menu based program with the following array-based queue function
ID: 3883329 • Letter: C
Question
C++ Implement a menu based program with the following array-based queue functions:
ADD (an element to the end of queue)
DELETE (an element from the front of queue)
SHOW (all elements, starting at first, ending at last)
COUNT (total number of elements)
CLEAR (initialize queue)
trade-offs between the two representations:
Using the count will make IsEmpty() and IsFull() easy, although we'll have to make sure the count is updated properly in ADD() and DELETE(). We haven't explored how to determine emptiness/fullness with rear or whether doing so presents any challenges.
Using the count in ADD() is similar to using rear. In both cases, the front won't move. With the count, we'll have to use count to calculate at which position to add new values, making sure to wrap around the array if necessary. Similarly, with a rear we have to make sure the rear moves properly and wraps around.
Using the count in DELETE() is not more difficult than using rear. In both cases, the front has to move (and wrap around when necessary).
In your homework assignment you can use whichever representation seems the easiest /most logical to you.
A hint: a very useful auxiliary function to have would be next_ix (que_index). It will give you the next queue index (ix++ or 1 depending where in the queue you are). This will save you a lot of if statements in the long run.
Explanation / Answer
#include<iostream>
#include<conio.h>
#include<stdlib.h>
class que
{
int arr[5],co=0;
int rear,front;
public:
que()
{
rear=-1;
front=-1;
}
void add(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
arr[++rear]=x;
cout <<"Added Successfully" <<x;
}
void delete()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted successfully" <<arr[++front];
}
void show()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<arr[i]<<" ";
}
void count()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
{co++;}
cout <<"Total number of elememnts"<<co-1;
}
void clear()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
{delete();}
}
};
void main()
{
int ch;
que qu;
while(1)
{
cout <<" 1.insert 2.delete 3.show 4.count 5.Clear 6.exit Enter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"Enter the number you want to add";
cin >> ch;
qu.add(ch);
break;
case 2: qu.delete(); break;
case 3: qu.show();break;
case 4: qu.count();break;
case 5: qu.clear();break;
case 6: exit(0);
}
}
return (0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.