-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS_Queue.py
112 lines (79 loc) · 3.05 KB
/
DS_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from collections import deque
###### DEQUE OBJECTS ######
class Queue_Deque:
def __init__(self):
self.queue = deque() # create a new deque() object upon class object creation
# Add items to queue in order from left to right
def enqueue(self, data):
return self.queue.append(data)
# Retrieve first added item (leftmost item) & remove from deque object
def dequeue(self):
return self.queue.popleft()
# Retrieve first added item, but DO NOT remove from deque
def peek(self):
return self.queue[0]
# Check to see if deque object is empty; returns a boolean of True if it is empty
def is_empty(self):
return len(self.queue) == 0
# Returns the number of items in the deque object
def size(self):
return len(self.queue)
# What to print out when Queue_Deque object name is invoked
def __repr__(self):
if self.is_empty():
return "No items have been added to deque yet"
else:
items = []
index = 0
max_index = self.size() - 1
while index <= max_index:
# First item added
if index == 0:
items.append("[FRONT: %s]" % self.queue[index])
# Last item added
elif index == max_index:
items.append("[BACK: %s]" % self.queue[max_index])
# All the rest of the items
else:
items.append("[%s]" % self.queue[index])
index += 1
return ' --> '.join(items)
###### LIST OBJECTS ######
class Queue_List:
def __init__(self):
self.queue = [] # create a new list upon class object creation
# Add items to list in order from left to right
def enqueue(self, data):
return self.queue.append(data)
# Retrieve first added item (leftmost item) & remove from list
def dequeue(self):
return self.queue.pop(0)
# Retrieve first added item, but DO NOT remove from list
def peek(self):
return self.queue[0]
# Check to see if list is empty; returns a boolean of True if it is empty
def is_empty(self):
return len(self.queue) == 0
# Returns the number of items in the list
def size(self):
return len(self.queue)
# What to print out when Queue_List object name is invoked
def __repr__(self):
if self.is_empty():
return "No items have been added to deque yet"
else:
items = []
index = 0
max_index = self.size() - 1
while index <= max_index:
# First item added
if index == 0:
items.append("[FRONT: %s]" % self.queue[index])
# Last item added
elif index == max_index:
items.append("[BACK: %s]" % self.queue[max_index])
# All the rest of the items
else:
items.append("[%s]" % self.queue[index])
index += 1
return ' --> '.join(items)