-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
58 lines (50 loc) · 1014 Bytes
/
stack.py
File metadata and controls
58 lines (50 loc) · 1014 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from ll import Node
# STACK FILO
class Stack(object):
def __init__(self):
self.head = None
def push(self, data):
n = Node(data)
n.next = self.head
self.head = n
return self.head
def pop(self):
if self.is_empty():
return None
data = self.head.data
self.head = self.head.next
return data
def is_empty(self):
return (self.head == None)
def peek(self):
return self.head.data if self.head else None
def peek_node(self):
return self.head if self.head else None
def main():
stack = Stack()
print("is empty?")
print(stack.is_empty())
stack.push(3)
stack.push(5)
stack.push(7)
stack.push(9)
stack.push(2)
stack.push(5)
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
data = stack.peek()
print(data)
print("is empty?")
print(stack.is_empty())
if __name__ == "__main__":
main()