In: Computer Science
Perhaps you remember the popular TV show, Deal or No Deal, with Howie Mandel as the host, that ran several years ago. In honor of the show returning in its original form (and renewed for a fifth season in 2019!) this lab is called Stack or No Stack.
Imagine again a bag data structure into which we can insert and remove elements. This bag has two operations, defined as:
1 x | Insert an element (with value x) into the bag |
2 x | Take an element from the bag (the value x) |
This time the bag is somewhat mysterious. Given a sequence of inserts and removals, the bag may operate like a LIFO stack, or it may not.
Your program must guess whether or not the bag is operating as a stack given a series of operations and the corresponding return values.
Program Input
The input to your program will be a series of test cases in a file. Each test case begins with a line containing a single integer n (1 < n < 100). Following the operations defined in the above table, each of of the next n lines is either a type-1 command followed by an integer x (which inserts the value x) or a type-2 command followed by an integer x which means the command retrieves the value x. The value of x is always a positive integer not larger than 100. There will be no ambiguous test cases. In other words, you will be able to fully determine the output based on the input alone. The input is terminated by an end-of-file (EOF).
Your code may use anything in the standard C++ library -- including the STL stack container. This is, of course, optional.
Program Output
For each test case, output one of the following:
stack not stack
which will indicate whether or not the bag is determined to be operating as a stack, given the series of operations.
Your code should compile, run, and pass all of the below test cases.
Your program should output only 'stack' or 'not stack' once per line, with one line for each test case.
Sample Input
Create a file with the following lines and use it as input into your program. Please use at least these as your test cases before submitting your code in Canvas.
4 1 2 1 1 2 1 2 2 6 1 5 1 10 1 12 2 10 2 5 2 12 2 1 8 2 8
Sample Output
stack not stack stack
code
#include <iostream>
#include <stack>
#include<fstream>
using namespace std;
int main ()
{
stack<int> s;
int size;
ifstream inFile;
bool isStack;
inFile.open("input.txt");
int num,command;
while(inFile>>size)
{
isStack=true;
for(int i=0;i<size;i++)
{
inFile>>command;
inFile>>num;
if(command==1)
s.push(num);
if(command==2)
{
if(s.top()==num)
s.pop();
else
{
isStack=false;
}
}
}
if(isStack)
cout<<"stack"<<endl;
else
cout<<"not
stack"<<endl;
}
}
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.