-
Notifications
You must be signed in to change notification settings - Fork 1
/
Implement-two-stacks-in-an-array.py
62 lines (53 loc) · 1.7 KB
/
Implement-two-stacks-in-an-array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
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
Implementation of twoStack should be space efficient.
'''
class twoStacks:
def __init__(self,n): #Constructor
self.size = n
self.arr = [None]*n
self.top1 = -1
self.top2 = self.size
def push1(self,x):
if self.top1 < self.top2 - 1:
self.top1 = self.top1 + 1
self.arr[self.top1] = x
else:
print "Stack Overflow"
exit(1)
def push2(self,x):
if self.top2 -1 > self.top1:
self.top2 = self.top2 - 1
self.arr[self.top2] = x
else:
print "Stack Overflow"
exit(1)
def pop1(self):
if self.top1 >= 0:
x = self.arr[self.top1]
self.top1 = self.top1 - 1
return x
else:
print "Stack underflow"
exit(1)
def pop2(self):
if self.top2 < self.size:
x = self.arr[self.top2]
self.top2 = self.top2 + 1
return x
else:
print "Stack underflow"
exit()
ts = twoStacks(5)
ts.push1(5)
ts.push2(10)
ts.push2(15)
ts.push1(11)
ts.push2(7)
print "Popped element from stack1 is " + str(ts.pop1())
ts.push2(40)
print "Popped element from stack2 is " + str(ts.pop2())