-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_queue.py
More file actions
55 lines (42 loc) · 1.67 KB
/
custom_queue.py
File metadata and controls
55 lines (42 loc) · 1.67 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
#!usr/bin/env python3
class CustomQueue:
def __init__(self):
self.items = []
def enqueue(self, item):
"""Takes in an item and inserts that item into the 0th index
of the list that is representing the CustomQueue.
The runtime is O(n), or linear time, because inserting into the 0th
index of a list forces all the other items in the list to move
one index to the right.
"""
self.items.insert(0, item)
def dequeue(self):
"""Returns and removes the front-most item of the CustomQueue, which is
represented by the last item in the list.
The runtime is O(1), or constant time, because indexing to the
end of a list happens in constant time.
"""
if self.items:
return self.items.pop()
return None
def peek(self):
"""Returns the last item in the list. which represents the
front-most item in the CustomQueue.
The runtime is constant because we're just indexing to the
last item of the list and returning the value found there.
"""
if self.items:
return self.items[-1]
return None
def size(self):
"""Returns the size of the CustomQueue, which is represent by the length of
the list.
The runtime is O(1), or constant time, because we're only
returning the length."""
return len(self.items)
def is_empty(self):
"""Returns a Boolean value expressing whether or not the list
representing the CustomQueue is empty.
Runs in constant time, because it's only checking for equality.
"""
return self.items == []