forked from bloominstituteoftechnology/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
128 lines (99 loc) · 3.31 KB
/
queue.py
File metadata and controls
128 lines (99 loc) · 3.31 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
A queue is a data structure whose primary purpose is to store and
return elements in First In First Out order.
1. Implement the Queue class using an array as the underlying storage structure.
Make sure the Queue tests pass.
2. Re-implement the Queue class, this time using the linked list implementation
as the underlying storage structure.
Make sure the Queue tests pass.
3. What is the difference between using an array vs. a linked list when
implementing a Queue?
Stretch: What if you could only use instances of your Stack class to implement the Queue?
What would that look like? How many Stacks would you need? Try it!
"""
# Array
# class Queue:
# def __init__(self):
# self.size = 0
# self.storage = []
# def __len__(self):
# return len(self.storage)
# def enqueue(self, value):
# self.storage.insert(0, value)
# def dequeue(self):
# if len(self.storage) == 0:
# return
# return self.storage.pop(-1)
# Re-implemented the queue class with linked list
class Queue:
def __init__(self):
self.size = 0
self.storage = LinkedList()
def __len__(self):
return self.size
def enqueue(self, value):
self.size += 1
self.storage.add_to_tail(value)
def dequeue(self):
if self.size == 0:
return None
self.size -= 1
value = self.storage.remove_head()
return value
# Linked List
class Node:
def __init__(self, value=None, next_node=None):
self.value = value
self.next_node = next_node
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def add_to_head(self, value):
new_node = Node(value)
if self.head is None and self.tail is None:
self.head = new_node
self.tail = new_node
else:
new_node.next_node = self.head
self.head = new_node
def add_to_tail(self, value):
new_node = Node(value)
if self.head is None and self.tail is None:
self.head = new_node
self.tail = new_node
else:
self.tail.next_node = new_node
self.tail = new_node
def remove_head(self):
if not self.head:
return None
if self.head.next_node is None:
head_value = self.head.value
self.head = None
self.tail = None
return head_value
head_value = self.head.value
self.head = self.head.next_node
return head_value
def contains(self, value):
if self.head is None:
return False
current_node = self.head
while current_node is not None:
if current_node.value == value:
return True
current_node = current_node.next_node
return False
def get_max(self):
if self.head is None:
return
current_node = self.head
max_value = 0
while current_node is not None:
if current_node.value > max_value:
print('current',current_node.value)
max_value = current_node.value
print('max', max_value)
current_node = current_node.next_node
return max_value