Q. An algorithm requires two stacks of sizes M and N that are to be maintained in the memory. Illustrate with an example how will you adjust two stacks in one dimensional array with M + N memory locations so that the overflow condition is minimized.


Answer :-

Let us say that stack A is with size M and stack B is with size N.

If the stack A is stored in locations 0 to M-1 and the stack B is stored in locations M to M + N - 1 the separate areas for the two are ensured. In the beginning the TOPs of both stacks are at opposite ends. When TOP. A reaches at M-1, any further insertion will lead to overflow in stack A and when TOP.B reaches at M, any further insertion in stack B will lead to overflow.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post