Site icon TestingDocs.com

Two Stacks in an Array

Problem Statement

Implement Two Stacks in an array. Implementation details are same like normal stack operations.

# You can push items to one of the chosen stack say stack1 or stack2.

# If the chosen stack is not empty, you may pop items from the stack.

# Print items in the array

# Implement methods like isEmpty(), isFull() , overflow, underflow etc.

Solution

I would provide some hints and the major part of the solution to the problem is left as an exercise for the reader. Consider the following diagram of the two stacks in the array.

 

 

For some of the methods you can specify the stack on which the methods operate. Obviously, you need two tops that point the top of the stacks housed in the array. Think of the methods and the logic for the isEmpty(), isFull() etc and conditions like overflow, underflow. A simple class diagram is as shown below.

 

The above diagram is just for reference. Feel free to change and give more meaningful method names and signatures.

Exit mobile version