Implement a class named stack pair that provides a pair of stacks. Make the class a...

80.2K

Verified Solution

Question

Programming

Implement a class named stack pair that provides a pair ofstacks. Make the class a template class. So, you will have twofiles: stack pair.h and stack pair.template, following the style ofthe text. The basic idea is that two stacks can share a singlestatic array. This may be advantageous if only one of the stackswill be in heavy use at any one time.

• The class should have various methods to manipulate thestack:

T pop a()

T pop b()

These methods pop the top of the respective stack and return thevalue. Use assert to cause the program to fail if an attempt ismade to pop from an empty list.

• void push a(T item)

void push b(T item)

These methods push the value onto the respective stack. Useassert to cause the program to fail if an attempt is made to pushto a full stack.

• size t size a()

size t size b()

These methods return the size of the respective stack. 1

• bool is empty a()

bool is empty b()

These methods return true if the respective stack is empty andreturn false if the stack is not empty.

• is full a()

is full b()

These methods return true if the respective stack has no morespace for new data to be pushed.

The methods return false if the respective stack has spaceavailable. Because of the implementation, these two methods willalways return the same thing. The implementation of the stackshould be done with a single static array. Define the CAPACITY ofthat array as a static constant of size 30, to start with.Variables will be needed to keep track of the top of each stack,we’ll call them top a and top b. For illustrations, we’ll use thefollowing diagram. The CAPACITY is only 16. Notice that top a andtop b indicate where the next element will go when pushing.

Test your work by writing four programs. Two of these will becopies of others. When you are instructed to check something, usean if-statement and, if the expected behavior fails, print amessage and terminate the program using the exit function. Write aprogram that will push a few values onto the a stack, then push afew values onto the b stack. The program should pop the values onthe a stack and check that they are correct as they are popped. Theprogram should pop the values on the b stack and check that theyare correct as they are popped. Copy the previous program andincrease the number of values pushed on a and or b to get a totalnumber of CAPACITY values pushed between the two stacks. Theprogram should not fail and both stack-full functions should returntrue. Copy the previous program and push one more value onto one ofthe stacks. The program should fail. Write a program that willcheck that popping from an empty stack terminates the program.

Answer & Explanation Solved by verified expert
4.1 Ratings (758 Votes)
stackpairh ifndef STACKPAIRH define STACKPAIRH include using namespace std template class stackpair private int topa topb public static variable declaration static const int CAPACITY static T arr constructor    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