-
Notifications
You must be signed in to change notification settings - Fork 50
/
queue.py
91 lines (70 loc) · 3.13 KB
/
queue.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
#!python
from linkedlist import LinkedList
# Implement LinkedQueue below, then change the assignment at the bottom
# to use this Queue implementation to verify it passes all tests
class LinkedQueue(object):
def __init__(self, iterable=None):
"""Initialize this queue and enqueue the given items, if any."""
# Initialize a new linked list to store the items
self.list = LinkedList()
if iterable is not None:
for item in iterable:
self.enqueue(item)
def __repr__(self):
"""Return a string representation of this queue."""
return 'Queue({} items, front={})'.format(self.length(), self.front())
def is_empty(self):
"""Return True if this queue is empty, or False otherwise."""
# TODO: Check if empty
def length(self):
"""Return the number of items in this queue."""
# TODO: Count number of items
def enqueue(self, item):
"""Insert the given item at the back of this queue.
Running time: O(???) – Why? [TODO]"""
# TODO: Insert given item
def front(self):
"""Return the item at the front of this queue without removing it,
or None if this queue is empty."""
# TODO: Return front item, if any
def dequeue(self):
"""Remove and return the item at the front of this queue,
or raise ValueError if this queue is empty.
Running time: O(???) – Why? [TODO]"""
# TODO: Remove and return front item, if any
# Implement ArrayQueue below, then change the assignment at the bottom
# to use this Queue implementation to verify it passes all tests
class ArrayQueue(object):
def __init__(self, iterable=None):
"""Initialize this queue and enqueue the given items, if any."""
# Initialize a new list (dynamic array) to store the items
self.list = list()
if iterable is not None:
for item in iterable:
self.enqueue(item)
def __repr__(self):
"""Return a string representation of this queue."""
return 'Queue({} items, front={})'.format(self.length(), self.front())
def is_empty(self):
"""Return True if this queue is empty, or False otherwise."""
# TODO: Check if empty
def length(self):
"""Return the number of items in this queue."""
# TODO: Count number of items
def enqueue(self, item):
"""Insert the given item at the back of this queue.
Running time: O(???) – Why? [TODO]"""
# TODO: Insert given item
def front(self):
"""Return the item at the front of this queue without removing it,
or None if this queue is empty."""
# TODO: Return front item, if any
def dequeue(self):
"""Remove and return the item at the front of this queue,
or raise ValueError if this queue is empty.
Running time: O(???) – Why? [TODO]"""
# TODO: Remove and return front item, if any
# Implement LinkedQueue and ArrayQueue above, then change the assignment below
# to use each of your Queue implementations to verify they each pass all tests
Queue = LinkedQueue
# Queue = ArrayQueue