Create a data structure twoStacks that represents two stacks. Implementation of twoStacks should use only one array, i.e., both stacks should use the same array for storing elements. Following functions must be supported by twoStacks.

push1(int x) –> pushes x to first stack
push2(int x) –> pushes x to second stack

pop1() –> pops an element from first stack and return the popped element
pop2() –> pops an element from second stack and return the popped element

[ad type=”banner”]

Implementation of twoStack should be space efficient.

Method 1 (Divide the space in two halves)
A simple way to implement two stacks is to divide the array in two halves and assign the half half space to two stacks, i.e., use arr[0] to arr[n/2] for stack1, and arr[n/2+1] to arr[n-1] for stack2 where arr[] is the array to be used to implement two stacks and size of array be n.

The problem with this method is inefficient use of array space. A stack push operation may result in stack overflow even if there is space available in arr[]. For example, say the array size is 6 and we push 3 elements to stack1 and do not push anything to second stack2. When we push 4th element to stack1, there will be overflow even if we have space for 3 more elements in array.

Method 2 (A space efficient implementation)
This method efficiently utilizes the available space. It doesn’t cause an overflow if there is space available in arr[]. The idea is to start two stacks from two extreme corners of arr[]. stack1 starts from the leftmost element, the first element in stack1 is pushed at index 0. The stack2 starts from the rightmost corner, the first element in stack2 is pushed at index (n-1). Both stacks grow (or shrink) in opposite direction. To check for overflow, all we need to check is for space between top elements of both stacks. This check is highlighted in the below code.

[ad type=”banner”]

Python Programming:

[pastacode lang=”python” manual=”%23%20Python%20Script%20to%20Implement%20two%20stacks%20in%20a%20list%0Aclass%20twoStacks%3A%0A%20%20%20%20%20%0A%20%20%20%20def%20__init__(self%2C%20n)%3A%20%20%20%20%20%23constructor%0A%20%20%20%20%20%20%20%20self.size%20%3D%20n%0A%20%20%20%20%20%20%20%20self.arr%20%3D%20%5BNone%5D%20*%20n%0A%20%20%20%20%20%20%20%20self.top1%20%3D%20-1%0A%20%20%20%20%20%20%20%20self.top2%20%3D%20self.size%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%23%20Method%20to%20push%20an%20element%20x%20to%20stack1%0A%20%20%20%20def%20push1(self%2C%20x)%3A%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20There%20is%20at%20least%20one%20empty%20space%20for%20new%20element%0A%20%20%20%20%20%20%20%20if%20self.top1%20%3C%20self.top2%20-%201%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.top1%20%3D%20self.top1%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20self.arr%5Bself.top1%5D%20%3D%20x%0A%20%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Stack%20Overflow%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20exit(1)%0A%20%0A%20%20%20%20%23%20Method%20to%20push%20an%20element%20x%20to%20stack2%0A%20%20%20%20def%20push2(self%2C%20x)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20There%20is%20at%20least%20one%20empty%20space%20for%20new%20element%0A%20%20%20%20%20%20%20%20if%20self.top1%20%3C%20self.top2%20-%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.top2%20%3D%20self.top2%20-%201%0A%20%20%20%20%20%20%20%20%20%20%20%20self.arr%5Bself.top2%5D%20%3D%20x%0A%20%0A%20%20%20%20%20%20%20%20else%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20print(%22Stack%20Overflow%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20exit(1)%0A%20%0A%20%20%20%20%23%20Method%20to%20pop%20an%20element%20from%20first%20stack%0A%20%20%20%20def%20pop1(self)%3A%0A%20%20%20%20%20%20%20%20if%20self.top1%20%3E%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20x%20%3D%20self.arr%5Bself.top1%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20self.top1%20%3D%20self.top1%20-1%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20x%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Stack%20Underflow%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20exit(1)%0A%20%0A%20%20%20%20%23%20Method%20to%20pop%20an%20element%20from%20second%20stack%0A%20%20%20%20def%20pop2(self)%3A%0A%20%20%20%20%20%20%20%20if%20self.top2%20%3C%20self.size%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20x%20%3D%20self.arr%5Bself.top2%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20self.top2%20%3D%20self.top2%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20x%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Stack%20Underflow%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20exit()%0A%20%0A%23%20Driver%20program%20to%20test%20twoStacks%20class%0Ats%20%3D%20twoStacks(5)%0Ats.push1(5)%0Ats.push2(10)%0Ats.push2(15)%0Ats.push1(11)%0Ats.push2(7)%0A%20%0Aprint(%22Popped%20element%20from%20stack1%20is%20%22%20%2B%20str(ts.pop1()))%0Ats.push2(40)%0Aprint(%22Popped%20element%20from%20stack2%20is%20%22%20%2B%20str(ts.pop2()))%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Sunny%20Karira%0A” message=”” highlight=”” provider=”manual”/]

Output:

  Popped element from stack1 is 11
  Popped element from stack2 is 40

Time complexity of all 4 operations of twoStack is O(1).
We will extend to 3 stacks in an array in a separate post.

[ad type=”banner”]

Categorized in: