In this article, we are going to discuss Python data structure Queue Class 12 Computer Science. As we have already discussed the implementation of Stack using the list. Similarly, Queue is also one of the data structures of Python. So let’s begin!
Introduction to Python Data Structure Queue Class 12
So let’s start Python Data Structure Queue Class 12 with an introduction. As you have seen queues at many places such as at ticket booking windows, in the morning assembly, at the bank ATM, at fee counter, in the shopping malls, vehicles at petrol pump etc. The python data structure queue is also following the same pattern and mechanism to store data.
The queue is another data structure like a stack. It is a linear (a sequence of the list of items) data structure. It follows a different mechanism to add or remove elements from the queue. It follows FIFO (First in First Out) mechanism to insert or remove elements. The elements can be inserted from one end and remove from another end.
So it requires two pointers:
- Front – An element always removed from the front end in the Queue
- Rear – An element inserted from rear end in the queue
Related terms while processing queue
The following terms are associated with queue:
- Front and Rear pointer as we discussed.
- Peek is the process of inspecting the element using front pointer.
- Overflow is the condition when your queue is full.
- Underflow is the condition when your queue is empty.
- Enqueue refers to the process of inserting an element in the queue.
- Dequeue refers to the process of deleting an element from the queue.
Watch this video for more understanding:
Implementation of Queue in python using list
When you want to implement Queue in Python the following steps to be required:
- Create a queue and take variables for the front and rear pointer.
- Initially, both pointers should be 0.
- Check the underflow/overflow condition as and when there are no elements in the queue or the queue is empty otherwise queue is full.
- Now when the insertion is done, the rear pointer index has been changed every time.
- When any element removed from the queue the front pointer index has been changed every time.
Creating Menu, Queue, and Initializing front and rear pointers
The first step is to create a menu, queue, and initialize front and rear for Python Data Structure Queue Class 12 program. Observe this code:
def main_menu(): while True: print("\nQueue Implementation") print("1 - EnQueue") print("2 - DeQueue") print("3 - Peek") print("4 - Display") print("5 - Exit") ch = int(input("Enter the your choice:")) if ch==1: ele=int(input("Enter Item:")) enQue(queue,ele) elif ch==2: ele=deQue(queue) if ele=="Undewrflow": print("Underflow!Queue is empty!") else: print("Dequeue-ed element is:", ele) elif ch==3: ele=peek(que) if ele=="Underflow": print("Queue is empty!") else: print("The element peeked is:",ele) elif ch==4: display(queue) elif ch==5: break else: print("-") main_menu()
Check underflow or isempty condition
You need to write a function to check the queue is empty or underflow condition. Observe this code:
def isEmpty(que): if que==: return True else: return False
Enqueue or insert element
To insert an element check the overflow condition and then use que.append(<item>) method. The item must be a rear pointer. When you are inserting the first element then the front and rear pointer index should be equal i.e. 0. Observe this code.
def enQue(que,item): que.append(item) if len(que)==1: front=rear=0 else: rear=len(que)-1
Now follow this code for calling function:
ele=int(input("Enter Item:")) enQue(queue,ele)
Dequeue or delete element
To delete, remove or dequeue elements you need to check the underflow condition. When there is a single element in the queue front and rear pointer index should be equal i.e. 0. Observe this code:
def deQue(que): if isEmpty(que): return "Underflow" else: def deQue(que): if isEmpty(que): return "Underflow" else: item=que.pop(0) if len(que)==0: front=rear=None return item
Observe this code for calling function deQue():
ele=deQue(queue) if ele=="Undewrflow": print("Underflow!Queue is empty!") else: print("Dequeue-ed element is:", ele)
Peek or inspect element
Peek or inspect element refers to the process of inspecting the value at the queue’s front without removing it. In Queue data structure the front is a point which hold value the position of first value in the queue. Observe the following function:
def peek(que): def peek(que): if isEmpty(que): return "Underflow" else: front=0 return que[front]
To call this function observe the code:
ele=peek(que) if ele=="Underflow": print("Queue is empty!") else: print("The element peeked is:",ele)
Display the queue
To display the elements in the queue after enqueue or dequeue you have to see the value of front and read pointers and the position of the element. When there is only a single element front and read will be the same only. Then use for loop starting from 1 to rear and print the element. Observe the code:
def display(que): if isEmpty(que): print("Queue Empty:") elif len(que)==1: print("front->", que," rear",end=" ") else: front=0 rear=len(que)-1 print("front->",que[front],end=" ") for i in range(1, rear): print(que[i],end=" ") print(que[rear],"<-rear")
Now observe this code for calling display() function.
Watch this video for practical demo:
Follow this link to download the complete program of implementation of a queue.
So I hope you enjoyed the article. If you have any doubt or query, feel free to ask in the comment section. Thank you for reading this article.