In: Computer Science
Please complete absolutely follow the requirements. Thanks!
Implement a stack ADT by writing a class called Stack. Use a static array to hold stack elements. Instantiate the Stack class in the main function and provide a user loop and a menu so that all the Stack class member-functions, push, pop, etc., are available so that the user can thoroughly exercise the member-functions of the Stack class. Also, implement a ReversePrint() for the stack. My StackProject, whose exposition I have given you, already has implemented a Print(), as well as Push() and Pop(). Also, implement a Roll() operator, which, when executed, interchanges (i.e., swaps) the top most two elements in an instance of the stack class; that is, a stack object. Thus, roll interchanges the topmost element with the second element from the top of the stack. Ask yourself the question and answer it: what is the stack interface?
Greetings!!!!!!!
Please find the Program and few screen shots of sample run.
#include <iostream>
using namespace std;
#define MAXSize 10
class Stack
{
int top;
public:
// Maximum size of the Stack
int stk[MAXSize];
Stack() { top = -1; }
/* bool push(int element);
int pop();
bool Roll();
int peek();
bool ReversePrint();
bool isEmpty(); */
bool push(int element)
{
if (top >= (MAXSize - 1)) {
cout << "Stack is full (Overflow)";
return false;
}
else {
top++;
stk[top] = element;
return true;
}
}
int pop()
{
if (isEmpty())
{
cout << "Stack is empty (Underflow)";
return 0;
}
else
{
int element = stk[top];
top--;
return element;
}
}
bool isEmpty()
{
if (top<0)
return true;
else
return false;
}
bool ReversePrint()
{
if (isEmpty())
{
cout << "Stack is empty (Underflow)";
return false;
}
else
{
cout<<"\nElement of stack in reverse order are:";
for(int loopcounter=top; loopcounter>=0; loopcounter--)
cout<<" "<<stk[loopcounter];
return true;
}
}
bool Roll()
{
if(top<=0)
{
return false;
}
else
{
int temp;
temp=stk[top-1];
stk[top-1]=stk[top];
stk[top]=temp;
cout <<"\nTop two elements " << stk[top] <<" and " << stk[top-1]<< " of the stack are rolled \n";
return true;
}
}
};
int main()
{
int choice, itemVal;
Stack obj;
do
{
cout<<"\n\n---------------- Stack Interface-----------\n";
cout<<"************** 1) Push in stack *************\n";
cout<<"************** 2) Pop from stack **************\n";
cout<<"************** 3) Print in reverse order **************\n";
cout<<"************** 4) Roll Opeation *****************\n";
cout<<"************** 5) Exit ****************\n";
cout<<"\nEnter choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"\nEnter value to be pushed: ";
cin>>itemVal;
if(obj.push(itemVal))
cout <<"\nElement " << itemVal << " is pushed into stack\n";
break;
}
case 2:
{
int popElment;
popElment=obj.pop();
if(popElment!=0)
cout <<"\nElement " << popElment << " is popped from the stack\n";
break;
}
case 3:
{
obj.ReversePrint();
break;
}
case 4:
{
int indexRoll=obj.Roll();
if(!indexRoll)
cout << "\nStack contains less than two elements, So Roll operation is not possible";
break;
}
case 5:
{
cout<<"Exit\n";
exit(1);
//break;
}
default:
{
cout<<"Invalid Choice"<<endl;
}
}
}while(choice!=5);
return 0;
}
Updated code as per comment (Contains main function at top) is as below.
#include <iostream>
using namespace std;
#define MAXSize 10
class Stack
{
int top;
public:
// Maximum size of the Stack
int stk[MAXSize];
Stack() { top = -1; }
bool push(int element);
int pop();
bool Roll();
bool ReversePrint();
bool isEmpty();
};
int main()
{
int choice, itemVal;
Stack obj;
do
{
cout<<"\n\n---------------- Stack Interface-----------\n";
cout<<"************** 1) Push in stack *************\n";
cout<<"************** 2) Pop from stack **************\n";
cout<<"************** 3) Print in reverse order **************\n";
cout<<"************** 4) Roll Opeation *****************\n";
cout<<"************** 5) Exit ****************\n";
cout<<"\nEnter choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"\nEnter value to be pushed: ";
cin>>itemVal;
if(obj.push(itemVal))
cout <<"\nElement " << itemVal << " is pushed into stack\n";
break;
}
case 2:
{
int popElment;
popElment=obj.pop();
if(popElment!=0)
cout <<"\nElement " << popElment << " is popped from the stack\n";
break;
}
case 3:
{
obj.ReversePrint();
break;
}
case 4:
{
int indexRoll=obj.Roll();
if(!indexRoll)
cout << "\nStack contains less than two elements, So Roll operation is not possible";
break;
}
case 5:
{
cout<<"Exit\n";
exit(1);
//break;
}
default:
{
cout<<"Invalid Choice"<<endl;
}
}
}while(choice!=5);
return 0;
}
bool Stack::push(int element)
{
if (top >= (MAXSize - 1)) {
cout << "Stack is full (Overflow)";
return false;
}
else {
top++;
stk[top] = element;
return true;
}
}
int Stack::pop()
{
if (isEmpty())
{
cout << "Stack is empty (Underflow)";
return 0;
}
else
{
int element = stk[top];
top--;
return element;
}
}
bool Stack::isEmpty()
{
if (top<0)
return true;
else
return false;
}
bool Stack::ReversePrint()
{
if (isEmpty())
{
cout << "Stack is empty (Underflow)";
return false;
}
else
{
cout<<"\nElement of stack in reverse order are:";
for(int loopcounter=top; loopcounter>=0; loopcounter--)
cout<<" "<<stk[loopcounter];
return true;
}
}
bool Stack::Roll()
{
if(top<=0)
{
return false;
}
else
{
int temp;
temp=stk[top-1];
stk[top-1]=stk[top];
stk[top]=temp;
cout <<"\nTop two elements " << stk[top] <<" and " << stk[top-1]<< " of the stack are rolled \n";
return true;
}
}