Question: A program needs two stacks of positive integers: one containing elements with values less than or equal to 1000 and the other containing elements

A program needs two stacks of positive integers: one containing elements with values less than or equal to

A program needs two stacks of positive integers: one containing elements with values less than or equal to 1000 and the other containing elements with values larger than 1000. The total number of elements in the small-value stack and the large-value stack combined is never more than 500 at any time, but we cannot predict how many will be in each stack. (Initially both will be empty; later on the stacks could be evenly divided, or all the elements could be in the small-value stack, and so on.) For efficiency reasons, we want to implement both stacks using a single array of size 500. Can you think of a way to do this? (a) Sketch what this "double-stack" array would look like. Explain how push and pop operations would work, accordingly. (3 marks) (b) Describe the private member variables of such a "double-stack" array based class, give their initial values, and explain how they are modified by push and pop operations. Coding is not required. (3 marks)

Step by Step Solution

3.46 Rating (153 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Initially top1 1 top2 500 Push inserts by increasing top1 for stack1 and by decreasing top2 f... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!