-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
58 lines (48 loc) · 1.29 KB
/
Copy pathstack.py
File metadata and controls
58 lines (48 loc) · 1.29 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
class Stack:
def __init__(self):
self.head = None
self.size = 0
def push(self, data):
new_node = Node()
new_node.data = data
next_node = self.head
self.head = new_node
self.head.next = next_node
self.size += 1
def pop(self):
if self.is_empty():
return None
else:
first_element = self.head
self.head = first_element.next
self.size -= 1
return first_element.data
def peek(self):
if self.is_empty():
return None
else:
return self.head.data
def size(self):
return self.size
def __str__(self):
return_str = ''
current_node = self.head
while current_node is not None:
return_str += str(current_node.data) + ' '
current_node = current_node.next
return return_str
def is_empty(self):
return self.head is None
class Node:
def __init__(self):
self.data = None
self.next = None
if __name__ == '__main__':
test_stack = Stack()
test_stack.push('a')
test_stack.push('b')
test_stack.push('c')
print(test_stack)
first_item = test_stack.pop()
print('popped ' + str(first_item))
print(test_stack)