Question
MyStack: Class Variables: list - a pointer that holds the memory address for an array of integers. top - an integer that stores the index
MyStack:
Class Variables:
list - a pointer that holds the memory address for an array of integers.
top - an integer that stores the index of the array element storing the top value of the stack. A value of -1 means the stack is empty, and a value of max - 1 means the stack is full.
max - the number of elements in the array whose address is stored in list.
Class Methods:
Constructor - accepts an integer as it's only argument. Initializes max with the argument and then dynamically allocates a new array of max elements, storing it's address in list. Initializes top to -1.
Destructor - frees all memory used by the MyStack object.
push - pushes it's argument onto the stack. Returns 0 if successful, -1 otherwise. The push operation will fail if the stack is full.
pop - removes the top value from the stack. Returns 0 if successful, -1 otherwise. Fails if the stack is empty.
peek - assigns the top value of the stack to it's reference parameter if the stack is not empty. Assigns nothing to the reference parameter if the stack is empty. Returns 0 if the stack isn't empty, -1 otherwise.
Test your implementation by adding to and removing values from the data structure. Use peek or first to check the values are being removed in the correct order.
No exception handling is required in this assignment.
Here's a driver you can use to get you started on your testing:
#include "MyStack.h"
#include
#include
int main()
{
MyStack s(5);
for(int i = 0; i
{
int x = 1 + rand() % 100;
std::cout
s.push(x);
}
std::cout
for(int i = 0; i
{
int x;
s.peek(x);
std::cout
s.pop();
}
std::cout
}
It is not complete and not a guarantee of a perfect score.
MyStack MyQueue -list: int* front: int back: int max: int -list: int* top: int -max: int MyStack(int m): MyStack(): +push(int a): int +pop(): int peek(int& a): int MyQueue(int m): MyQueue(): +enqueue(int a): int +dequeue(): int +first (int& a): intStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started