For this assignment you will implement stacks and queues ADT. A stack and a queu
ID: 3605047 • Letter: F
Question
For this assignment you will implement stacks and queues ADT. A stack and a
queue are the same if they have the same number of elements and the elements
at the corresponding positions are the same. The program displays a menu on
the screen allowing the user to enter the elements of stack and queue. The
program terminates when the user enters 9.
MUST FOLLOW THESE INSTRUCTIONS:
You are not allowed to use stack / queue libraries. You must implement all
stack / queue functions and operations .
Must use templates to implement this program.
MUST BE DONE IN C++
VALIDATIONS:
The program accepts only integer data type when selecting from the menu (1 or 9).
Everything else should be rejected with an invalid option message.
SAMPLE RUN OF HOW THE PROGRAM SHOULD LOOK LIKE:
*** Welcome to my stack - queue program ***
The function of this program is to validate
that a stack and a queue are identical.
Stacks/queues are same if they have the
same number of elements and their elements
at the corresponding positions are the same.
Select from the following menu
1. Enter stack/queue values.
9. Terminate the program.
Enter your choice from the menu ----> abc
** Invalid choice **
Enter your choice from the menu ----> 5
** Invalid choice **
Enter your choice from the menu ----> 123
** Invalid choice **
Enter your choice from the menu ----> ;
** Invalid choice **
Enter your choice from the menu ----> 1
Enter stack values terminated by ; 123;
Enter queue values terminated by ; 123;
Stack and Queue are identical
Select from the following menu
1. Enter stack/queue values.
9. Terminate the program.
Enter your choice from the menu ----> 1
Enter stack values terminated by ; 123;
Enter queue values terminated by ; 132;
Stack and Queue are not identical
Select from the following menu
1. Enter stack/queue values.
9. Terminate the program.
Enter your choice from the menu ----> 1
Enter stack values terminated by ; abc;
Enter queue values terminated by ; ABC;
Stack and Queue are not identical
Select from the following menu
1. Enter stack/queue values.
9. Terminate the program.
Enter your choice from the menu ----> 1
Enter stack values terminated by ; 12345;
Enter queue values terminated by ; abcdefghijklmnOP;
Stack and Queue are not identical
Select from the following menu
1. Enter stack/queue values.
9. Terminate the program.
Enter your choice from the menu ----> x
** Invalid choice **
Enter your choice from the menu ----> 9
*** Program is terminated. ***
Explanation / Answer
// C++ program to implement a stack using
// single queue
#include<bits/stdc++.h>
using namespace std;
// User defined stack that uses a queue
class Stack
{
queue<int>q;
public:
void push(int val);
void pop();
int top();
bool empty();
};
// Push operation
void Stack::push(int val)
{
// Get previous size of queue
int s = q.size();
// Push current element
q.push(val);
// Pop (or Dequeue) all previous
// elements and put them after current
// element
for (int i=0; i<s; i++)
{
// this will add front element into
// rear of queue
q.push(q.front());
// this will delete front element
q.pop();
}
}
// Removes the top element
void Stack::pop()
{
if (q.empty())
cout << "No elements ";
else
q.pop();
}
// Returns top of stack
int Stack::top()
{
return (q.empty())? -1 : q.front();
}
// Returns true if Stack is empty else false
bool Stack::empty()
{
return (q.empty());
}
// Driver code
int main()
{
Stack s;
s.push(10);
s.push(20);
cout << s.top() << endl;
s.pop();
s.push(30);
s.pop();
cout << s.top() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.