Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18 Stacks...

70.2K

Verified Solution

Question

Statistics

Code needed in C++, make changes to the file provided (18-1, has3 files)

Chapter 18 Stacks and Queues

-----------------------------------------------------------------------------------------------------

capacity is just 5

1. push 6 numbers on the stack

2. catch the overflow error in a catch block

3. pop one element, which means your capacity is now down to4

4. push the element that was rejected earlier

5. verify your entire stack by popping to show the newnumbers.

IntStack.h

#include

using namespace std;

class IntStack

{

unique_ptrstackArray;

int capacity;

int top;

public:

// Constructor

IntStack(int capacity);

// Member functions

void push(int value);

void pop(int &value);

bool isEmpty() const;

// Stack Exceptions

class Overflow {};

class Underflow {};

};

IntStack.cpp

ZOOM

#include \"intstack.h\"

//************************************

// Constructor *

//************************************

IntStack::IntStack(int capacity)

{

stackArray = make_unique(capacity);

this->capacity = capacity;

top = 0;

}

//***********************************

// Adds a value to the stack *

//***********************************

void IntStack::push(int value)

{

if (top == capacity) throw IntStack::Overflow();

stackArray[top] = value;

top++;

}

//****************************************

// Determines whether the stack is empty *

//****************************************

bool IntStack::isEmpty() const

{

return top == 0;

}

//************************************************

// Removes a value from the stack and returns it *

//************************************************

void IntStack::pop(int &value)

{

if (isEmpty()) throw IntStack::Underflow();

top--;

value = stackArray[top];

}

pr18-01.cpp

// This program illustrates the IntStack class.

#include \"intstack.h\"

#include

using namespace std;

int main()

{

IntStack stack(5);

int values[] = { 5, 10, 15, 20, 25 };

int value;

cout << \"Pushing...\n\";

for (int k = 0; k < 5; k++)

{

cout << values[k] << \" \";

stack.push(values[k]);

}

cout << \"\nPopping...\n\";

while (!stack.isEmpty())

{

stack.pop(value);

cout << value << \" \";

}

cout << endl;

return 0;

}

Answer & Explanation Solved by verified expert
4.2 Ratings (777 Votes)
note use compiler stdc14 intStackh include using namespace stdclass IntStackuniqueptrstackArrayint capacityint toppublic ConstructorIntStackint capacity Member    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students