. 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.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 1TF
icon
Related questions
Question

. 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.

Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning