Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MyStack: Class Variables: list - a pointer that holds the memory address for an array of integers. top - an integer that stores the index

image text in transcribed

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): int

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

Students also viewed these Databases questions

Question

What would you do?

Answered: 1 week ago

Question

Prepare a short profile of Henry words worth Longfellow?

Answered: 1 week ago

Question

What is RAM as far as telecommunication is concerned?

Answered: 1 week ago

Question

Question 1: What is reproductive system? Question 2: What is Semen?

Answered: 1 week ago

Question

Describe the sources of long term financing.

Answered: 1 week ago

Question

7. How might you go about testing these assumptions?

Answered: 1 week ago