-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
63 lines (52 loc) · 1.56 KB
/
stack.py
File metadata and controls
63 lines (52 loc) · 1.56 KB
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
59
60
61
62
63
from node import Node
class Stack:
def __init__(self, limit=1000):
self.top_item = None
self.size = 0
self.limit = limit
def push(self, value):
if self.has_space():
item = Node(value)
item.set_next_node(self.top_item)
self.top_item = item
self.size += 1
print("Adding {} to the pizza stack!".format(value))
else:
print("No room for {}!".format(value))
def pop(self):
if not self.is_empty():
item_to_remove = self.top_item
self.top_item = item_to_remove.get_next_node()
self.size -= 1
print("Delivering " + item_to_remove.get_value())
return item_to_remove.get_value()
print("All out of pizza.")
def peek(self):
if not self.is_empty():
return self.top_item.get_value()
print("Nothing to see here!")
def has_space(self):
return self.limit > self.size
def is_empty(self):
return self.size == 0
# # Defining an empty pizza stack
# pizza_stack = Stack(6)
# # Adding pizzas as they are ready until we have
# pizza_stack.push("pizza #1")
# pizza_stack.push("pizza #2")
# pizza_stack.push("pizza #3")
# pizza_stack.push("pizza #4")
# pizza_stack.push("pizza #5")
# pizza_stack.push("pizza #6")
# # Uncomment the push() statement below:
# pizza_stack.push("pizza #7")
# # Delivering pizzas from the top of the stack down
# print("The first pizza to deliver is " + pizza_stack.peek())
# pizza_stack.pop()
# pizza_stack.pop()
# pizza_stack.pop()
# pizza_stack.pop()
# pizza_stack.pop()
# pizza_stack.pop()
# # Uncomment the pop() statement below:
# pizza_stack.pop()