[Type C]Q5. Write a program to insert or delete an element from a queue depending upon the user’s choice.

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 program to insert or delete an element from a queue depending upon the user’s choice.

Program code:

Output:

[1]
[1, 2]
[1, 2, 3]
1
2
Final list after dequeue: [3]

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