Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the stackADT class and stackType classes given to you in Ch. 18 and code supplied here to complete this assignment. #include #include myStack.h using

Use the stackADT class and stackType classes given to you in Ch. 18 and code supplied here to complete this assignment.

#include

#include "myStack.h"

using namespace std;

int main()

{

stackType stack1(50);

stackType stack2(50);

stack1.initializeStack();

stack1.push(23);

stack1.push(45);

stack1.push(38);

stack2 = stack1;

if (stack1 == stack2)

cout

else

cout

stack2.pop();

stack2.push(32);

cout

if (stack1 == stack2)

cout

else

cout

stack2.push(11);

cout

if (stack1 == stack2)

cout

else

cout

return 0;

}

Modify the stack implementation such that it uses a vectors as the container instead of an array.

Overload the pure virtual function push such that it takes a vector as an argument which pushes multiple values onto the stack.

Throw exceptions when:

An attempt is made to pop from an empty stack (display message)

An attempt is made to push onto a full stack (display message)

top() is called on an empty stack (display message).

A negative or zero value for stack size is passed to the constructor (In this case, when handling the error, automatically recall the constructor with a value of 100).

Using the STL queue container, add multiple stackType classes to a queue. Demonstrate an ability to use STL queue methods such as front() and pop().

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

stack Operations - initializeStack: Initializes the stack to an empty state. - isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true ; otherwise, it returns the value false . - isFullStack: Determines whether the stack is full. If the stack is full, it returns the value true ; otherwise, it returns the value false . - push: Adds a new element to the top of the stack. The input to this operation consists of the stack and the new element. Prior to this operation, the stack must exist and must not be full. - top : Returns the top element of the stack. Prior to this operation, the stack must exist and must not be empty. - pop: Removes the top element of the stack. Prior to this operation, the stack must exist and must not be empty. The function copyStack makes a copy of a stack. The stack to be copied is passed as a parameter to the function copyStack. We will, in fact, use this function to implement the copy constructor and overload the assignment operator. The definition of this function is as follows: Details Program code. In the code, the words in the variable names are merged. Line 1: template . Line 2: void stack type, , colon, colon, copy stack, left parenthesis, c o n s t stack type , ampersand, other stack, left parenthesis. Line 3: left brace. Line 4: delete, open bracket, close bracket, list, semi colon. Line 5: m a x stack size, = other stack, period, m a x stack size, semi colon. Line 6: stack top = other stack, period, stack top, semi colon. Line 7: list = new type, open bracket, m a x stack size, close bracket, semi colon. Line 8 , indented once: forward slash, forward slash, copy other stack into this stack. Line 9: for, left parenthesis, in t j=0, semi colon, j j++, right parenthesis. Line 10, indented once: list, open bracket, j, close bracket = other stack, period, list, open bracket, j, close bracket, semi colon. Line 11: right brace, forward slash, forward slash, end copy stack. Full Stack Next, we consider the operation isFullStack. It follows that the stack is full if stackTop is equal to maxStackSize. The definition of the function isFullStack is as follows: template bool stackType: : isfullstack() const { return (stackTop == maxStacksize); \} //end isfullstack Details Program code. In the code, the words in the variable names are merged. Line 1: template . Line 2: b o o l stack type , colon, colon, is full stack, left parenthesis, right parenthesis, c o n s t. Line 3: left brace. Line 4, indented once: return, left parenthesis, stack top ==m a x stack size, right parenthesis, semi colon. Line 5: right brace, forward slash, forward slash, end is full stack. Push Adding, or pushing, an element onto the stack is a two-step process. Recall that the value of stackTop indicates the number of elements in the stack, and stackTop - 1 gives the position of the top element of the stack. Therefore, the push operation is as follows: 1. Store the newItem in the array component indicated by stackTop. 2. Increment stackTop. Figures 18-8 and 18-9 illustrate the push operation. Return the Top Element The operation top returns the top element of the stack. Its definition is as follows: stack Operations - initializeStack: Initializes the stack to an empty state. - isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true ; otherwise, it returns the value false . - isFullStack: Determines whether the stack is full. If the stack is full, it returns the value true ; otherwise, it returns the value false . - push: Adds a new element to the top of the stack. The input to this operation consists of the stack and the new element. Prior to this operation, the stack must exist and must not be full. - top : Returns the top element of the stack. Prior to this operation, the stack must exist and must not be empty. - pop: Removes the top element of the stack. Prior to this operation, the stack must exist and must not be empty. Pop Now we consider the pop operation, which removes the top element of the stack. Consider the stack shown in Figure 18-15. Figure 18-15. Stack before the pop operation Figure 18-16 shows the pop operation

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions