-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack.py
More file actions
43 lines (33 loc) · 799 Bytes
/
Stack.py
File metadata and controls
43 lines (33 loc) · 799 Bytes
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
#Implementation of Stack using Python Lists
class Stack:
def __init__(self):
self._A = []
def __len__(self):
return len(self._A)
def is_empty(self):
return len(self._A)==0
def push(self, item):
self._A.append(item)
def pop(self):
if self.is_empty():
return ("Stack is empty")
return self._A.pop()
def top(self):
if self.is_empty():
return ("Stack is empty")
return self._A[-1]
S = Stack()
S.push(5)
print("Top is " + str(S.top()))
S.push(10)
print("Top is " + str(S.top()))
S.push(15)
print("Top is " + str(S.top()))
S.pop()
print("Top is " + str(S.top()))
S.pop()
print("Top is " + str(S.top()))
S.pop()
print("Top is " + str(S.top()))
S.pop()
print("Top is " + str(S.top()))