-
Notifications
You must be signed in to change notification settings - Fork 2
/
Stack.py
56 lines (47 loc) · 1.66 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
#This program illustrates an example of Stack implementation
#Stack Operations: push(), pop(), isEmpty(), peek(), stackSize()
class Stack(object):
def __init__(self, size):
self.index = []
self.size = size
def __str__(self):
myString = ' '.join(str(i) for i in self.index)
return myString
def push(self, data):
''' Pushes a element to top of the stack '''
if(self.isFull() != True):
self.index.append(data)
else:
print('Stack overflow')
def pop(self):
''' Pops the top element '''
if(self.isEmpty() != True):
return self.index.pop()
else:
print('Stack is already empty!')
def isEmpty(self):
''' Checks whether the stack is empty '''
return len(self.index) == []
def isFull(self):
''' Checks whether the stack if full '''
return len(self.index) == self.size
def peek(self):
''' Returns the top element of the stack '''
if(self.isEmpty() != True):
return self.index[-1]
else:
print('Stack is already empty!')
def stackSize(self):
''' Returns the current stack size '''
return len(self.index)
if __name__ == '__main__':
myStack = Stack(10)
for i in range(0, 10):
myStack.push(i)
print(myStack.isEmpty()) # False
print(myStack.isFull()) # True
print(myStack) # 0 1 2 3 4 5 6 7 8 9
print(myStack.stackSize()) # 10
print(myStack.pop()) # 9
print(myStack) # 0 1 2 3 4 5 6 7 8
print(myStack.peek()) # 8