Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called Stack with the following members: a. A private integer arr

ID: 3594068 • Letter: C

Question

Create a class called Stack with the following members:

a. A private integer array called stack that contains STACK_SIZE elements. You will only use this array in your constructor and st functions. Do NOT use it directly with any of your other member functions!

b. A private integer pointer called stack_pointer.

c. A public constructor. This constructor will set the stack_pointer to the bottom of the stack. Remember that stacks grow from bottom to top!

d. A public void function called push. This function takes one integer parameter which is the value to be pushed on the stack. This function will push the given value onto the stack.

e. A public function called pop. This function takes no parameters, but returns an integer. This function will pop the value given by the stack pointer.

f. A public function called sp. This function takes no parameters, but returns an integer pointer. This function will return the stack pointer variable.

g. A public function called st. This function takes no parameters, but returns an integer pointer. This function will return the TOP of the stack array. Remember that the stack grows from bottom to top. This function always returns the address of the top (the first element of the stack array).

NOTE: Notice that there are no if statements, conditionals, or any boolean logic for a stack. It is up to the user to make sure that they do not exceed the bounds of the stack. As such, you must not have any sanity checks in your code!

Explanation / Answer

#include <iostream>
#include <stdlib.h>
using namespace std;
#define STACK_SIZE 10
//Creates a class Stack
class Stack
{
private:
//To store data
int mstack[STACK_SIZE];
// To point to top
int top;
int *stack_pointer;

public:
//Default constructor
Stack()
{
// top is initialized to -1
top = -1;
// Stack pointer points to the top
stack_pointer = &top;
}//End of constructor
//Function to display stack contents
void displayStack()
{
cout<<" Stack contents: ";
//Loops till end of stack
for(int y = top; y >= 0; y--)
cout<<mstack[y]<<", ";
}//End of function
// Function to push an item
void push(int item)
{
//Checks for full
if(*sp() == STACK_SIZE - 1)
{
cout<<endl<< "Stack is full";
return;
}//End of if
//Increase the top position by one
top++;
//Adds the item to the top of the stack
mstack[top] = item;
}//End of function
//Function to pop an element from the stack
int pop()
{
//Checks for empty
if(*st() == -1)
{
cout<<endl<< "Stack is empty";
exit(0);
}
//Extracts data from stack top
int data = mstack[top];
//Decrease the stack top by one
top--;
return data;
}//End of function

//Function to return stack top
int *sp()
{
return stack_pointer;
}//End of function
//Function to return stack top pointer
int *st()
{
return stack_pointer;
}//End of function
};//End of class
//main function definition
int main()
{
//Creates a stack object
Stack s;
//Push element to the stack
s.push(10);
s.push(20);
s.push(30);
s.displayStack();
//Removes the top element from stack
int i = s.pop();
cout<<endl<< "Popped = "<<i<<endl;
//Removes the top element from stack
i = s.pop();
cout<<endl<< "Popped = "<<i<<endl;

return 0;
}//End of function

Sample Run:


Stack contents:
30, 20, 10,
Popped = 30

Popped = 20

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote