Two stacks of positive integers are needed, one containing elements with values less than or equal to 1,000 and the other containing elements with values larger than 1,000. The total number of elements in the small-value stack and the large-value stack combined are not more than 200 at any time, but we cannot predict how many are in each stack. (All of the elements 2 could be in the small-value stack, they could be evenly divided, both stacks could be empty, and so on.). 1. Can you think of a way to implement both stacks in one array?
Two stacks of positive integers are needed, one containing elements with
values less than or equal to 1,000 and the other containing elements with values larger than 1,000.
The total number of elements in the small-value stack and the large-value stack combined are not
more than 200 at any time, but we cannot predict how many are in each stack. (All of the elements
2
could be in the small-value stack, they could be evenly divided, both stacks could be empty, and
so on.).
1. Can you think of a way to implement both stacks in one array?
Let S[] be the array we will use.
It is given that at any instant, S cannot have more than 200 elements.
So we will create our array of size 200.
So we declare S[200]. Let the index range from 1 to 200 (not 0 to 199, for convenience )
Trending now
This is a popular solution!
Step by step
Solved in 6 steps