World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Neha Kumawat
a year ago
class Queue:
def __init__(self):
self.queue = list()
def addtoq(self,dataval):
# Insert method to add element
if dataval not in self.queue:
self.queue.insert(0,dataval)
return true
return false
def size(self):
return len(self.queue)
TheQueue = Queue()
TheQueue.addtoq("JAN")
TheQueue.addtoq("FEB")
TheQueue.addtoq("MARCH")
print(TheQueue.size())
3
class Queue:
def __init__(self):
self.queue = list()
def addtoq(self,dataval):
# Insert method to add element
if dataval not in self.queue:
self.queue.insert(0,dataval)
return true
return false
# Pop method to remove element
def removefromq(self):
if len(self.queue)>0:
return self.queue.pop()
return ("No elements in Queue!")
q = Queue()
q.addtoq("JAN")
q.addtoq("FEB")
q.addtoq("Wed")
print(q.removefromq())
print(q.removefromq())
JAN
FEB
# Python program to
# demonstrate queue implementation
# using list
# Initializing a queue
q = []
# Adding elements to the queue
q.append('a')
q.append('b')
q.append('c')
print("Initial queue")
print(q)
# Removing elements from the queue
print("\nElements dequeued from queue")
print(q.pop(0))
print(q.pop(0))
print(q.pop(0))
print("\nQueue after removing elements")
print(q)
# Uncommenting print(q.pop(0))
# will raise and IndexError
# as the queue is now empty
Initial queue
['a', 'b', 'c']
Elements dequeued from queue
a
b
c
Queue after removing elements
[]
Traceback (most recent call last):
File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in
print(queue.pop(0))
IndexError: pop from empty list
# Python program to
# demonstrate queue implementation
# using collections.dequeue
from collections import deque
# Initializing a queue
queue = deque()
# Adding elements to a queue
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue")
print(queue)
# Removing elements from a queue
print("\nElements dequeued from the queue")
print(queue.popleft())
print(queue.popleft())
print(queue.popleft())
print("\nQueue after removing elements")
print(queue)
# Uncommenting queue.popleft()
# will raise an IndexError
# as queue is now empty
Initial queue
deque(['a', 'b', 'c'])
Elements dequeued from the queue
a
b
c
Queue after removing elements
deque([])
Traceback (most recent call last):
File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in
q.popleft()
IndexError: pop from an empty deque
# Python program to
# demonstrate implementation of
# queue using queue module
from queue import Queue
# Initializing a queue
queue = Queue(maxsize = 3)
# qsize() give the maxsize
# of the Queue
print(queue.qsize())
# Adding of element to queue
queue.put('x')
queue.put('y')
queue.put('z')
# Return Boolean for Full
# Queue
print("\nFull: ", queue.full())
# Removing element from queue
print("\nElements dequeued from the queue")
print(queue.get())
print(queue.get())
print(queue.get())
# Return Boolean for Empty
# Queue
print("\nEmpty: ", queue.empty())
queue.put(1)
print("\nEmpty: ", queue.empty())
print("Full: ", queue.full())
# This would result in Infinite
# Loop as the Queue is empty.
# print(queue.get())
0
Full: true
Elements dequeued from the queue
x
y
z
Empty: true
Empty: false
Full: false
class Node:
def __init__(self,data):
self.data=data
self.next=None
class Queue:
def __init__(self):
self.front=None
self.queueSize=0
def enQueue(self,data):
temp=Node(data)
if self.front is None:
self.front=temp
self.queueSize= self.queueSize+1
else:
curr=self.front
while curr.next!=None:
curr=curr.next
curr.next=temp
self.queueSize=self.queueSize+1
def deQueue(self):
try:
if self.front == None:
raise Exception("Queue is Empty")
else:
temp=self.front
self.front=self.front.next
tempdata=temp.data
self.queueSize= self.queueSize-1
del temp
return tempdata
except Exception as e:
print(str(e))
def front_element(self):
try:
if self.front == None:
raise Exception("Queue is Empty")
else:
return self.front.data
except Exception as e:
print(str(e))
def size(self):
return self.queueSize
def isEmpty(self):
if self.queueSize==0:
return True
else:
return False