[Type C]Q8. Write a python program that implements three queues namely HighestPr, NormalPr, and LowestPr.

What is a Queue?

Like stack, the queue is a linear data structure that stores items in First In First Out (FIFO) manner. With a queue, the least recently added item is removed first.

A good example of the queue is any queue of customers for a resource where the customer that came first receives the service first.

Basic operations on Queue:

1. Enqueue: Enqueue means inserting an element in the queue.

In a normal queue at a ticket counter, where does a new person go and stand to become a part of the queue?

The person goes and stands in the back.

Similarly, a new element in a queue is inserted at the back of the queue.

2. Dequeue: Dequeue means removing an element from the queue.

Since queue follows the FIFO principle we need to remove the element of the queue which was inserted at first.

Thus element at the front is removed and the element behind it becomes the new front element.

Problem: Write a python program that implements three queues namely HighestPr, NormalPr, and LowestPr.

The program accepts an element along with its priority from the user as follows:

Enter element: ComputerTutor

Enter Priority (Highest/Normal/Lowest(H/N/L)): H

As per priority entered, the element is added in the corresponding queue. A menu offers the following options:

1] Insert element

2] Search For element

3]Change Priority.

For option 1, the element is inserted in one queue as described above.

For option 2, ask for the element to be searched and search for it in all 3 queues. If found, display its queue name otherwise display “Element not found”.

For option 3, ask for an element, if it exists in a queue, ask for its new priority and add it to the directed queue.

Program code:

Output:

Welcome to Computer Tutor!

Your available options are:
 1. Insert Element
 2. Search an element
 3. Change Priority of an element
 4. Exit 
Option you want to choose: (1/2/3/4):1 
Enter an element: 5 
Priority (H/N/L): N
 Welcome to Computer Tutor! Your available options are: 1. Insert Element
2. Search an element 
3. Change Priority of an element 
4. Exit Option you want to choose: (1/2/3/4):2 
Enter element to be searched:5
5 is in NormalPr Queue 
Welcome to Computer Tutor! Your available options are: 1. Insert Element
2. Search an element 
3. Change Priority of an element 
4. Exit Option you want to choose: (1/2/3/4):3 
Enter element:5 
Do you want to increase or decrease the priority? (I/D):I 
Welcome to Computer Tutor! Your available options are: 1. Insert Element
2. Search an element 
3. Change Priority of an element 
4. Exit 
Option you want to choose: (1/2/3/4):4

In the above code snippet basic functions of Queue are demonstrated.

To view all the lessons in chapter 10: http://computertutor.in/class-12-computer-science/chapter-10/

To view entire class 12 computer science study material: http://computertutor.in/resources/

You cannot copy content of this page