-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathImmutable_Stack.py
More file actions
39 lines (31 loc) · 816 Bytes
/
Immutable_Stack.py
File metadata and controls
39 lines (31 loc) · 816 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
class Stack:
def __init__(self, elem = None, lst = None):
if lst is not None:
self.stack = Create_from_list(elem, lst)
else:
self.stack = Create_list([])
def push(self, elem):
return Stack(elem, self.stack)
def pop(self):
if self.stack.tail is not Nil:
return Stack(nth(1, self.stack), lself.stack.tail.tail)
else:
return Nil()
def is_empty(self):
return empty(self.stack)
def peek(self):
return nth(0, self.stack)
def __str__(self):
if self.stack is Nil:
return '()'
else:
return self.stack.__str__()
'''
s1 = Stack()
s2 = s1.push(1)
print(s2.is_empty())
print(s2)
outcome ->
False
(1)
'''