-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStack.py
More file actions
38 lines (29 loc) · 726 Bytes
/
Stack.py
File metadata and controls
38 lines (29 loc) · 726 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
#stacks can be easily implemented using arrays/linkedlist
class Stack:
def __init__(self):
self.Stack = [] # the stack is a one dimensional array
def isEmpty(self):
return self.Stack == []
def push(self,data):
# inserting items into self.stack
self.Stack.append(data)
def pop(self):
# first item in last out
data = self.Stack[-1]
del self.Stack[-1]
return data
def peek(self):
# returns the last inserted item
return self.Stack[-1]
def sizeStack(self):
return len(self.Stack)
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
print(stack.sizeStack())
print("popped :", stack.pop())
print("popped :", stack.pop())
print(stack.sizeStack())
print("peek :",stack.peek())