Site icon Study Algorithms

How will you implement 2 stacks using one array?

Question: How do we implement 2 stacks using only one array? Our stack routines should not indicate an exception unless every slot in the array is used?

SOLUTION:

Algorithm:

CAN WE EXTEND THIS METHOD FOR 3 STACKS?

Sure we can, but the possibility is limited by the size of the array, here is one scenario:-

To implement 3 stacks we keep the following information:-

Now let us try to define the PUSH and POP operations:-

PUSHING

POPING

Exit mobile version