Wednesday, August 6, 2008

c simple

Explain how to implement two stacks in one array in such a way that neither stack overflows unless the total number of elements in both the stacks together is n. The push operation must run in O(1) time...

2 comments:

Unknown said...

consider one array in both end points separate stack pointer named as SP1,SP2 and push any one end (it must be o(1)) if the total element is same for both stack,

___________________________
SP1___________________________SP2
|<.........array[].........>|
n elements

Srivignesh said...

construct 1 stack from array index 0..
construct 2 stack from array index n-1......