-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.py
58 lines (50 loc) · 1.11 KB
/
Stack.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
class Stack:
Top = -1
def __init__(self,size):
self.stack = [0 for i in range(size)]
def push(self, value):
if (Stack.Top!=len(self.stack)-1):
Stack.Top += 1
self.stack[Stack.Top] = value
else:
print("Overflow: Stack is Full")
def pop(self):
if (Stack.Top == -1):
print("Underflow: Stack is Empty")
else:
Stack.Top -= 1
def peek(self):
if (Stack.Top == -1):
print("Underflow: Stack is Empty")
else:
return (self.stack[Stack.Top])
def traverse(self):
if (Stack.Top == -1):
print("Underflow: Stack is Empty")
else:
for i in range(Stack.Top,-1,-1):
print(self.stack[i], end = " ")
def is_Full(self):
if (Stack.Top == len(self.stack)-1):
return True
else:
return False
def is_Empty(self):
if (Stack.Top == -1):
return True
else:
return False
# Driver Code
data = Stack(5)
data.traverse()
data.push(5)
data.push(36)
data.push(43)
data.traverse()
data.push(33)
data.traverse()
print(data.peek())
data.pop()
data.traverse()
print(data.is_Empty())
print(data.is_Full())