forked from learning-zone/python-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_queue_ll.py
175 lines (116 loc) · 3.44 KB
/
create_queue_ll.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
class Node(object):
""" Creates a node class. """
def __init__(self, data):
self.data = data
self.next = None
class Queue(object):
""" Creates a queue using a linked list. """
def __init__(self):
self.head = None
self.tail = None
def __repr__(self):
if not self.length():
return '<Queue (empty)>'
else:
return '<Queue %s>' % self.head
def length(self):
""" Gets length of queue.
>>> q = Queue()
>>> q.length()
0
"""
curr = self.head
length = 0
while curr is not None:
length += 1
curr = curr.next
return length
def enqueue(self, item):
""" Add item to end of queue::
>>> q = Queue()
>>> q.enqueue("buy flight")
>>> q.enqueue("pack")
>>> q.enqueue("enjoy vacation")
>>> q.print_queue()
['buy flight', 'pack', 'enjoy vacation']
>>> q.length()
3
"""
new_node = Node(item)
if self.head is None and self.tail is None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
def dequeue(self):
""" Add item to end of queue::
>>> q = Queue()
>>> q.enqueue("buy flight")
>>> q.enqueue("pack")
>>> q.enqueue("vacation")
>>> q.print_queue()
['buy flight', 'pack', 'vacation']
>>> q.dequeue()
'buy flight'
>>> q.print_queue()
['pack', 'vacation']
>>> q2 = Queue()
>>> q2.dequeue()
"""
if self.head is None:
return None
else:
dequeued = self.head.data
self.head = self.head.next
return dequeued
def is_empty(self):
""" True/false if queue is empty.
>>> q = Queue()
>>> q.enqueue("buy flight")
>>> q.enqueue("pack")
>>> q.enqueue("vacation")
>>> q.is_empty()
False
>>> q2 = Queue()
>>> q2.is_empty()
True
"""
return self.head is None
def peek(self):
""" Return but don't remove the first item in the queue.
>>> q = Queue()
>>> q.enqueue("buy flight")
>>> q.enqueue("pack")
>>> q.enqueue("enjoy vacation")
>>> q.peek()
'buy flight'
>>> q.print_queue()
['buy flight', 'pack', 'enjoy vacation']
"""
return self.head.data
def print_queue(self):
""" Prints items in queue in a list.
>>> q = Queue()
>>> q.enqueue("buy flight")
>>> q.enqueue("pack")
>>> q.enqueue("enjoy vacation")
>>> q.print_queue()
['buy flight', 'pack', 'enjoy vacation']
>>> q2 = Queue()
>>> q2.print_queue()
[]
"""
curr = self.head
if curr is None:
return list()
queue = []
while curr is not None:
queue.append(curr.data)
curr = curr.next
return queue
if __name__ == '__main__':
import doctest
results = doctest.testmod()
if not results.failed:
print "ALL TESTS PASSED!"