Thumbnail article
Simple queue in python
A queue is a linear data structure that stores objects in the order of First In First Out (FIFO). When using a queue, the item that was most recently added is removed first. The code below is the program of queue algorithm.
from queue import Queue

# Initializing a queue
q = Queue(maxsize = int(input("Input the size of queue :")))
x = 0

while(x != 3):
  x=int(input("choose operation 1. add, 2. remove 3. exit :"))
  if(x==1):
    if(q.full()):
      print("the queue is full")
    else:
      q.put(input("insert the value :"))
  elif(x==2):
    if(q.empty()):
      print("the queue is empty")
    else:
      q.get()
  	  
  print(q.queue)
q.put received 3 parameters like this q.put(item, block=True, timeout=None). q.put  is used to put item into the queue. If optional args block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).q.get received 2 parameters like this q.get(block=True, timeout=None). q.get remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).A good real life example of a queue implementation is a line of customers who waiting for a resource. When a customer arrives, the customer will head to the back of the queue. Every customer is served, the queue in front will move forward. If we are in the second queue, then we will wait for the first queue to process. When the process is finished from the first queue he will leave, and it’s our turn to go forward to carry out the process.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *