-
Notifications
You must be signed in to change notification settings - Fork 11
/
Queue.py
45 lines (36 loc) · 1.14 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
# Python implementation of a simple queue
from collections import deque
class Queue:
def __init__(self):
self.queue = deque()
def enqueue(self, value):
self.queue.append(value)
def dequeue(self):
if not self.isEmpty():
return self.queue.popleft() # Remove and return the first element
raise IndexError('Queue is empty')
def isEmpty(self):
return len(self.queue) == 0
def front(self):
if not self.isEmpty():
return self.queue[0] # Return the FRONT of the queue without removing it
raise IndexError('Queue is empty')
def rear(self):
if not self.isEmpty():
return self.queue[-1] # Return the REAR of the queue without removing it
raise IndexError('Queue is empty')
# Test Cases
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
assert queue.front() == 1
assert queue.rear() == 3
assert queue.dequeue() == 1
assert queue.front() == 2
assert queue.rear() == 3
assert queue.dequeue() == 2
assert queue.front() == 3
assert queue.rear() == 3
assert queue.dequeue() == 3
assert queue.isEmpty() == True