. Let S1 and S2 be two stacks. Write a procedure Push(x, S) that pushes element x onto stack S, where S is one or the other of these two stacks. Include all necessary error checks in your procedure.
. Let S1 and S2 be two stacks.
Write a procedure Push(x, S) that pushes element x onto stack S, where S is one or the other of these two stacks. Include all necessary error checks in your procedure.
First of all we should know why two stacks start from the opposite side.This is because so that we can utilize the stack to its maximum capacity.Suppose we have an array of size 10 and we partition it into 2 stacks,starting from index 0 and 6,now if if we keep on inserting elements in the first element then only 5 elements can be inserted.But, now if 2 stacks start from the opposite side then 10 elements can be filled.
Let ith index tells the top element of stack 1,and let jth index tells the top of stack 2.Now ,in order to insert the element we should always take care that there exits an empty space in the stack,which can be checked, if (j-i)>=1 .
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images