1. Linear Queue using Python List
In [1]:
Copied!
class Queue:
def __init__(self, max_size):
self.front = -1
self.rear = -1
self.capacity = max_size
self.items = [None] * max_size
self.count = 0
def __len__(self):
return self.count
def __repr__(self):
item_list = []
for idx in range(self.count):
if idx == 0:
if self.front == self.rear:
item_list.append("[Front: %s :Rear]" % self.items[idx])
else:
item_list.append("[Front: %s]" % self.items[idx])
elif idx == self.count - 1:
item_list.append("[%s: Rear]" % self.items[idx])
else:
item_list.append("[%s]" % self.items[idx])
return "<-".join(item_list)
def __iter__(self):
for i in range(self.count):
yield self.items[i]
def is_empty(self):
pass
def peek(self):
if self.front != -1:
return self.items[self.front]
def enqueue(self, data):
if self.rear >= self.capacity:
print("Queue is full, dequeue all the elements to re-use the queue")
return
if self.rear == self.front == -1:
print(" == Inserting first element == ")
print(f"Inserting: {data} into the queue")
self.front = self.rear = 0
self.items[self.rear] = data
else:
print(f"Inserting: {data} into the queue")
self.items[self.rear] = data
self.rear += 1
self.count += 1
def dequeue(self):
if self.front == self.rear:
if self.front == -1:
print("queue is empty, nothing to dequeue")
elif self.front == self.capacity:
print("reached end of queue")
self.front = self.rear = -1
else:
data = self.items[self.front]
print(f"Removing: {data} from the queue")
self.front = self.rear = -1
self.count -= 1
else:
data = self.items[self.front]
print(f"Removing: {data} from the queue")
self.front += 1
self.count -= 1
class Queue:
def __init__(self, max_size):
self.front = -1
self.rear = -1
self.capacity = max_size
self.items = [None] * max_size
self.count = 0
def __len__(self):
return self.count
def __repr__(self):
item_list = []
for idx in range(self.count):
if idx == 0:
if self.front == self.rear:
item_list.append("[Front: %s :Rear]" % self.items[idx])
else:
item_list.append("[Front: %s]" % self.items[idx])
elif idx == self.count - 1:
item_list.append("[%s: Rear]" % self.items[idx])
else:
item_list.append("[%s]" % self.items[idx])
return "<-".join(item_list)
def __iter__(self):
for i in range(self.count):
yield self.items[i]
def is_empty(self):
pass
def peek(self):
if self.front != -1:
return self.items[self.front]
def enqueue(self, data):
if self.rear >= self.capacity:
print("Queue is full, dequeue all the elements to re-use the queue")
return
if self.rear == self.front == -1:
print(" == Inserting first element == ")
print(f"Inserting: {data} into the queue")
self.front = self.rear = 0
self.items[self.rear] = data
else:
print(f"Inserting: {data} into the queue")
self.items[self.rear] = data
self.rear += 1
self.count += 1
def dequeue(self):
if self.front == self.rear:
if self.front == -1:
print("queue is empty, nothing to dequeue")
elif self.front == self.capacity:
print("reached end of queue")
self.front = self.rear = -1
else:
data = self.items[self.front]
print(f"Removing: {data} from the queue")
self.front = self.rear = -1
self.count -= 1
else:
data = self.items[self.front]
print(f"Removing: {data} from the queue")
self.front += 1
self.count -= 1
In [2]:
Copied!
queue = Queue(5)
queue = Queue(5)
In [3]:
Copied!
for i in range(1, 10):
queue.enqueue(i)
for i in range(1, 10):
queue.enqueue(i)
== Inserting first element == Inserting: 1 into the queue Inserting: 2 into the queue Inserting: 3 into the queue Inserting: 4 into the queue Inserting: 5 into the queue Queue is full, dequeue all the elements to re-use the queue Queue is full, dequeue all the elements to re-use the queue Queue is full, dequeue all the elements to re-use the queue Queue is full, dequeue all the elements to re-use the queue
In [4]:
Copied!
queue
queue
Out[4]:
[Front: 1]<-[2]<-[3]<-[4]<-[5: Rear]
In [5]:
Copied!
for i in range(10):
queue.dequeue()
for i in range(10):
queue.dequeue()
Removing: 1 from the queue Removing: 2 from the queue Removing: 3 from the queue Removing: 4 from the queue Removing: 5 from the queue reached end of queue queue is empty, nothing to dequeue queue is empty, nothing to dequeue queue is empty, nothing to dequeue queue is empty, nothing to dequeue
In [6]:
Copied!
queue
queue
Out[6]:
In [7]:
Copied!
len(queue)
len(queue)
Out[7]:
0
In [8]:
Copied!
for i in queue:
print(i)
for i in queue:
print(i)
In [9]:
Copied!
for i in range(1, 10):
queue.enqueue(i)
for i in range(1, 10):
queue.enqueue(i)
== Inserting first element == Inserting: 1 into the queue Inserting: 2 into the queue Inserting: 3 into the queue Inserting: 4 into the queue Inserting: 5 into the queue Queue is full, dequeue all the elements to re-use the queue Queue is full, dequeue all the elements to re-use the queue Queue is full, dequeue all the elements to re-use the queue Queue is full, dequeue all the elements to re-use the queue
In [10]:
Copied!
for i in range(10):
queue.dequeue()
for i in range(10):
queue.dequeue()
Removing: 1 from the queue Removing: 2 from the queue Removing: 3 from the queue Removing: 4 from the queue Removing: 5 from the queue reached end of queue queue is empty, nothing to dequeue queue is empty, nothing to dequeue queue is empty, nothing to dequeue queue is empty, nothing to dequeue
In [11]:
Copied!
queue
queue
Out[11]:
In [12]:
Copied!
for i in range(1, 10):
queue.enqueue(i)
for i in range(1, 10):
queue.enqueue(i)
== Inserting first element == Inserting: 1 into the queue Inserting: 2 into the queue Inserting: 3 into the queue Inserting: 4 into the queue Inserting: 5 into the queue Queue is full, dequeue all the elements to re-use the queue Queue is full, dequeue all the elements to re-use the queue Queue is full, dequeue all the elements to re-use the queue Queue is full, dequeue all the elements to re-use the queue
In [13]:
Copied!
queue
queue
Out[13]:
[Front: 1]<-[2]<-[3]<-[4]<-[5: Rear]
In [14]:
Copied!
queue.peek()
queue.peek()
Out[14]:
1
In [15]:
Copied!
for i in range(10):
queue.dequeue()
for i in range(10):
queue.dequeue()
Removing: 1 from the queue Removing: 2 from the queue Removing: 3 from the queue Removing: 4 from the queue Removing: 5 from the queue reached end of queue queue is empty, nothing to dequeue queue is empty, nothing to dequeue queue is empty, nothing to dequeue queue is empty, nothing to dequeue
In [16]:
Copied!
queue
queue
Out[16]:
In [17]:
Copied!
queue.peek()
queue.peek()
In [ ]:
Copied!