-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.py
117 lines (98 loc) · 2.9 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from module3_linked_lists.node import Node
class Stack():
def __init__(self):
self.top = None
self.size = 0
def push(self, data):
"""push Adds an element on top of the stack
Args:
data: Data to be added
"""
node = Node(data)
# Puts the data at the top of the stack
if self.top:
node.next = self.top
self.top = node
else: # Else makes the data the new node
self.top = node
self.size += 1
def pop(self):
"""pop Removes an element at the top of the stack
Returns:
data: Data removed from the stack
"""
# If there is data on the stack removes the data at the top
if self.top:
data = self.top.data
self.size -= 1
# In case there is more data on the stack
if self.top.next:
self.top = self.top.next
else:
self.top = None
return data
else:
return 'The stack is empty'
def peek(self):
"""peek Shows the data of the top of the stack
Returns:
data: Data at the top
"""
if self.top:
return self.top.data
else:
return 'The stack is empty'
def clear(self):
"""clear Empties the stack
"""
while self.top:
self.pop()
# * Challenges
def search_element(self, data):
node_returned = self.top
if self.size > 0:
while node_returned and node_returned.data != data:
node_returned = node_returned.next
if node_returned is None:
print(f"Data '{data}' was not found!\n")
else:
print(f"Data '{data}' was found!\n")
else:
print('\nThe stack is empty\n')
def iter(self):
print('_' * 3, 'Stack data', '_' * 3)
node_returned = self.top
print('Size:', self.size, 'elements')
while node_returned:
current = node_returned.data
node_returned = node_returned.next
print('\t', current)
def another_clear(self):
self.top = None
self.size = 0
print('Stack emptied')
""" Code use in the shell to test the code
from stack import Stack
food = Stack()
food.push('egg')
food.push('ham')
food.push('spam')
food.pop()
#? Expected: 'spam'
food.peek()
#? Expected: 'ham'
food.clear()
food.peek()
#? Expected: 'The stack is empty'
"""
if __name__ == '__main__':
food = Stack()
food.push('egg')
food.push('ham')
food.push('spam')
food.search_element('ham')
food.search_element('Nothing')
food.iter()
food.another_clear()
food.search_element('egg')
food.iter()