-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.mojo
74 lines (57 loc) · 1.96 KB
/
Stack.mojo
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
from python import Python
struct Stack:
var items: PythonObject
var py: PythonObject
fn __init__(inout self, items: PythonObject) raises:
self.items = items
self.py = Python.import_module("builtins")
fn add(inout self, new_item: Int64) raises:
_ = self.items.append(new_item) # Using the _ variable only to dump the value and avoid warnings.
fn is_empty(inout self) raises -> Bool:
if self.items == []:
return True
else:
return False
fn remove(inout self) raises:
if self.is_empty():
print("The Stack is empty, there's nothing to delete.")
else:
_ = self.items.pop(0) # Using the _ variable only to dump the value and avoid warnings.
fn clear(inout self) raises:
self.items = []
fn display(inout self):
print(self.items)
fn peek(inout self) raises -> PythonObject:
return self.items[0]
fn size(inout self) raises -> PythonObject:
return self.py.len(self.items)
"""
fn __iter__(inout self) raises:
var i = 0
while i < self.py.len(self.items): # Impossible to use at the moment since len return as PythonObject, not a Integer.
yield self.items[i]
i += 1
"""
fn main() raises:
var myStack = Stack([1, 2, 3, 4])
myStack.display()
print("The size of the Stack is", myStack.size())
print("")
myStack.add(5)
myStack.add(6)
print("Peeking at the first item...", myStack.peek())
myStack.display()
print("The size of the Stack is", myStack.size())
print("")
myStack.remove()
myStack.remove()
myStack.remove()
myStack.remove()
myStack.display()
print("The size of the Stack is", myStack.size())
print("")
myStack.clear()
print("Clearing Stack...")
print("The size of the Stack is", myStack.size())
print("Trying to remove another item...")
myStack.remove()