-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstack_ll.py
More file actions
109 lines (94 loc) · 2.6 KB
/
stack_ll.py
File metadata and controls
109 lines (94 loc) · 2.6 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
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
"""
Linked list implementation of a stack
Space used must be linear in the number of items in a stack
Stack() -> creates a new Stack object
.is_empty()
len() -> number of elements in stack
.push(item) -> push item into Stack
.pop() -> Remove and return the most recent item in the stack
"""
class Node:
"""
Node class to represent each single node
Mainly used as a linked list
"""
def __init__(self, item, next_item):
self.item = item
self.next_item = next_item
def __str__(self):
return str(self.item)
class Stack:
"""
Linked list implementation of a stack
"""
def __init__(self):
"""
Initialize the stack
"""
self.top = None
self.length = 0
def __len__(self):
"""
Returns the len of a stack
For example:
s = Stack()
len(s) gives 0
"""
return self.length
def __str__(self):
"""
Returns the string representation of a stack
"""
current = self.top
this_str = "Stack: Top of stack ["
while current is not None:
this_str += str(current)
if current.next_item is not None:
this_str += " -> "
current = current.next_item
this_str += "] Bottom of stack; length = {}".format(self.length)
return this_str
def is_empty(self):
"""
Returns True if the stack has no items and empty; Otherwise, False
"""
return self.length == 0
def push(self, item):
"""
Add item into the stack following first in last out (LIFO)
That is, the most recently added element is at the top of the stack.
Returns None
"""
# Reassign the link to top of the stack and update the
self.top = Node(item, self.top)
self.length += 1
def pop(self):
"""
Remove the top item in the stack
Returns the top item of the stack
"""
if self.top is None:
return None
else:
popped_node = self.top
# Reassign the top node
self.top = self.top.next_item
# Remove popped node next link
popped_node.next_item = None
# Update length
self.length -= 1
return popped_node
if __name__ == "__main__":
s = Stack()
# Test stack
for x in [1, 2, 3, 4 ,5]:
s.push(x)
print(s)
x = Stack()
while not s.is_empty():
x.push(s.pop())
#print("x", x)
#print("s", s)
s.push(None)
print("x", x)
print("s", s)