Python Implemented Queue [ code included ]
A Queue is one such data structure algorithm that every computer science student must learn it. It is used in so many places to implement such a crucial task that without stack it is almost unimaginable to run vital system of a computer.
Stacks are used in computer hardware like in keyboard, printer. .
A queueis present in almost everywhere in our daily system, though we used to implement it using so many languages, here in this article we will implement it using python list.
How Stack is implemented in Python
A queue is a data algorithm that is inspired by real-life containers. In a queue, every new element gets inserted at the last position and services are always served from the front. The same we used in python list to implement a queue. Whole Stack program is divided into four parts
- Function to insert a new element at the end in our list – add-element( )
- function to remove first element from the list – delete_element ( )
- Function to display queue content – display
All these functions are now invoked thorough a menu drive program to mimic Queue
Source code of Python-based Queue
#function to add element at the end of list def add_element(stack,x): stack.append(x) #function to remove last element from list def delete_element(stack): n = len(stack) if(n<=0): print("Queue empty....Deletion not possible") else: del(stack) #function to display stack entry def display(stack): if len(stack)<=0: print("Queue empty...........Nothing to display") for i in stack: print(i,end=" ") #main program starts from here x= choice=0 while (choice!=4): print("\n\tQueue menu \n\n\t1. Add Element \n\t2. Delete Element \n\t3. Display \n\t4. Exit") choice = int(input("\n Enter your choice : ")) if(choice==1): value = int(input("Enter value : ")) add_element(x,value) if(choice==2): delete_element(x) if(choice==3): display(x) if(choice==4): print("You selected to close this program")
Explanation of Python-based Queue
Since Python does not support array and link-list like C and C++, here we have implemented Queue using python list. python list support a lot of built-in methods like append() and del( )
append() method automatically add an element at the last position of the list and the size of list is only dependent on the memory size of your system. The below code is doing the same here in this push function( )
#function to add element at the end of list def add_element(stack,x): stack.append(x)
Here stack is the name of list and x is that element that we are actually inserting in the list.
del( ) is another list method that is used to remove the an element from the list but before removing any element from the list, just checking the present number of elements in the list.
#function to remove last element from list def delete_element(stack): n = len(stack) if(n<=0): print("Queue empty....Deletion not possible") else: del(stack)
The same has been implemented for displaying the stack content.
#function to display stack entry def display(stack): if len(stack)<=0: print("Stack empty...........Nothing to display") for i in stack: print(i,end=" ")
All these functions now have been invoked and the working of this whole program is behaving like a Queue. The output of queue program is as follow
$ C:/Python37/python.exe e:/python/DataStructure/queue.py Queue menu 1. Add Element 2. Delete Element 3. Display 4. Exit Enter your choice :
Well, there are some scope for improvement in this program, If you are able to find out that scope, please feel free to contact us via email.