Stack Implementation • Introduction: For this lab, you will write a program that
ID: 3621444 • Letter: S
Question
Stack Implementation• Introduction:
For this lab, you will write a program that implements a stack. This will entail creating a
stack data structure, operations on the stack, and a main loop. The stack data structure is
a set of memory locations that holds the stack content and all necessary information about
the stack (i.e. the address of the top of the stack, the maximum size of the stack, the
current size of the stack, etc). The stack operations you must support and the
input/output format are given below. Your stack should have a maximum size of 8.
• Specification:
1 Get a character from the user. This character will be the operation.
2 If the operation is + :
If the stack is full, print “Stack overflow!".
Otherwise, get a number from the user and push it onto the stack.
Loop back to 1.
3 If the operation is – :
If the stack is empty, display “Stack underflow!”.
Otherwise, pop a number from the top of the stack and print it.
Loop back to 1.
4 If the operation is p :
If the stack is empty, print “Stack empty.”
Otherwise, print the contents of the stack from top to bottom
(in the following format: position value).
Loop back to 1.
5 If the operation is e :
Empty the stack (no output is necessary) and loop back to 1.
6 If the operation is d :
Print the depth of the stack (i.e. the number of items on the stack).
(Only display the number--no other text)
Loop back to 1.
7 If the operation is q :
Print “Goodbye.” and quit the program.
8 If the operation is none of the above
Print “Invalid operation!” and loop back to 1.
Explanation / Answer
Dear,
//Header file section
#include < iostream >
using namespace std;
class IntStack
{
private:
int *stackArray; // Pointer to the stack array
int stackSize; // The stack size
int top; // Indicates the top of the stack
public:
// Constructor
IntStack(int);
// Copy constructor
IntStack(const IntStack &);
// Destructor
~IntStack();
// Stack operations
void push(int);
void pop(int &);
bool isFull() const;
bool isEmpty() const;
int size()const;
};
//***********************************************
// Constructor *
// This constructor creates an empty stack. The *
// size parameter is the size of the stack. *
//***********************************************
IntStack::IntStack(int size)
{
stackArray = new T[size];
stackSize = size;
top = -1;
}
//***********************************************
// Copy constructor *
//***********************************************
IntStack::IntStack(const IntStack &obj)
{
// Create the stack array.
if (obj.stackSize > 0)
stackArray = new T[obj.stackSize];
else
stackArray = NULL;
// Copy the stackSize attribute.
stackSize = obj.stackSize;
// Copy the stack contents.
for (int count = 0; count < stackSize; count++)
stackArray[count] = obj.stackArray[count];
// Set the top of the stack.
top = obj.top;
}
//***********************************************
// Destructor *
//***********************************************
IntStack::~IntStack()
{
delete [] stackArray;
}
//*************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************
void IntStack::push(int num)
{
if (isFull())
{
cout << "The stack is full. ";
}
else
{
top++;
stackArray[top] = num;
}
}
void IntStack::print()
{
if(!isEmpty())
{
for(int i=0;i<=top;i++)
cout<< stackArray[top]<<” “<}
else
cout<<”Stack is empty”;
}
Int IntStack::size()
{
return top;
}
//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************
void IntStack::pop(int &num)
{
if (isEmpty())
{
cout << "The stack is empty. ";
}
else
{
num = stackArray[top];
top--;
}
}
//***************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//***************************************************
bool IntStack::isFull() const
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
//****************************************************
// Member funciton isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
bool IntStack::isEmpty() const
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
int main()
{
int Var1; // To hold values popped off the stack
char choice;
IntStack stack1(5);
cout<<"Enter character ";
cin>>choice;
do
{
switch(choice)
{
case ‘+’: if(stack1.isFull())
cout<<”Stack Overflow”;
else{
cout<<"Enter element to push " ;
cin>>Var1;
cout << "Pushing ";
stack1.push(Var1);}
break;
case ‘-’: if(stack1.isEmpty())
cout<<”Stack Underflow”;
else{
cout << "Popping... ";
stack1.pop(Var1);
cout <break;
case ‘p’: stack1.print();
break;
case ‘e’: while(!stack1.isEmpty())
{
stack1.pop(Var1);
}
break;
case ‘d’: cout<<”Depth:”<break;
case ‘q’:cout<<”GoodBye”;
break;
default: cout<<”Invalid operation”;
}
cout<<"Enter character ";
cin>>choice;
}
while(choice!=’q’);
system("pause");
return 0;
}//End main
Hope this will help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.