2. Linear Queue using Linked List
In [1]:
Copied!
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
return "<Node: %s>" % s
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
return "" % s
In [2]:
Copied!
class Queue:
def __init__(self, max_size):
self.front = None
self.rear = None
self.count = 0
self.capacity = max_size
def __iter__(self):
ptr = self.front
while ptr is not None:
yield ptr.data
ptr = ptr.next
def __repr__(self):
temp = self.front
nodes = []
while temp:
if temp == self.front:
nodes.append("[Front: %s]" % temp.data)
elif temp == self.rear:
nodes.append("[Rear: %s]" % temp.data)
else:
nodes.append("[%s]" % temp.data)
temp = temp.next
return "<-".join(nodes)
def __len__(self):
return self.count
def peek(self):
if self.front is not None:
return self.front.data
def enqueue(self, data):
if self.count == self.capacity:
print("queue is full, dequeue all elements first")
return
node = Node(data)
if self.front == self.rear == None:
print("inserting first element in queue")
self.front = self.rear = node
else:
print(f"Inserting: {data} in the queue")
self.rear.next = node
self.rear = self.rear.next
self.count += 1
def dequeue(self):
if self.count == 0:
print("Queue is empty")
self.front = self.rear = None
elif self.front is not None:
data = self.front.data
print(f"Removing: {data} from the queue")
temp = self.front
self.front = self.front.next
del temp
self.count -= 1
class Queue:
def __init__(self, max_size):
self.front = None
self.rear = None
self.count = 0
self.capacity = max_size
def __iter__(self):
ptr = self.front
while ptr is not None:
yield ptr.data
ptr = ptr.next
def __repr__(self):
temp = self.front
nodes = []
while temp:
if temp == self.front:
nodes.append("[Front: %s]" % temp.data)
elif temp == self.rear:
nodes.append("[Rear: %s]" % temp.data)
else:
nodes.append("[%s]" % temp.data)
temp = temp.next
return "<-".join(nodes)
def __len__(self):
return self.count
def peek(self):
if self.front is not None:
return self.front.data
def enqueue(self, data):
if self.count == self.capacity:
print("queue is full, dequeue all elements first")
return
node = Node(data)
if self.front == self.rear == None:
print("inserting first element in queue")
self.front = self.rear = node
else:
print(f"Inserting: {data} in the queue")
self.rear.next = node
self.rear = self.rear.next
self.count += 1
def dequeue(self):
if self.count == 0:
print("Queue is empty")
self.front = self.rear = None
elif self.front is not None:
data = self.front.data
print(f"Removing: {data} from the queue")
temp = self.front
self.front = self.front.next
del temp
self.count -= 1
In [3]:
Copied!
queue = Queue(5)
queue = Queue(5)
In [4]:
Copied!
queue
queue
Out[4]:
In [5]:
Copied!
for i in range(10):
queue.enqueue(i)
for i in range(10):
queue.enqueue(i)
inserting first element in queue Inserting: 1 in the queue Inserting: 2 in the queue Inserting: 3 in the queue Inserting: 4 in the queue queue is full, dequeue all elements first queue is full, dequeue all elements first queue is full, dequeue all elements first queue is full, dequeue all elements first queue is full, dequeue all elements first
In [6]:
Copied!
for i in queue:
print(i)
for i in queue:
print(i)
0 1 2 3 4
In [7]:
Copied!
queue
queue
Out[7]:
[Front: 0]<-[1]<-[2]<-[3]<-[Rear: 4]
In [8]:
Copied!
queue.dequeue()
queue.dequeue()
Removing: 0 from the queue
In [9]:
Copied!
queue
queue
Out[9]:
[Front: 1]<-[2]<-[3]<-[Rear: 4]
In [10]:
Copied!
for _ in range(10):
queue.dequeue()
for _ 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 Queue is empty Queue is empty Queue is empty Queue is empty Queue is empty Queue is empty
In [11]:
Copied!
queue
queue
Out[11]:
In [12]:
Copied!
for i in range(10):
queue.enqueue(i)
for i in range(10):
queue.enqueue(i)
inserting first element in queue Inserting: 1 in the queue Inserting: 2 in the queue Inserting: 3 in the queue Inserting: 4 in the queue queue is full, dequeue all elements first queue is full, dequeue all elements first queue is full, dequeue all elements first queue is full, dequeue all elements first queue is full, dequeue all elements first
In [13]:
Copied!
queue
queue
Out[13]:
[Front: 0]<-[1]<-[2]<-[3]<-[Rear: 4]
In [14]:
Copied!
len(queue)
len(queue)
Out[14]:
5
In [15]:
Copied!
queue.peek()
queue.peek()
Out[15]:
0
In [16]:
Copied!
queue.dequeue()
queue.dequeue()
Removing: 0 from the queue
In [17]:
Copied!
queue.peek()
queue.peek()
Out[17]:
1
In [ ]:
Copied!