Queue
Queue — Study Notes
NCERT-aligned · 9 notes · 3 shown free
4.1 INTRODUCTION TO QUEUE
Explanation4.1 INTRODUCTION TO QUEUE
In this section, we introduce the Queue data structure, a fundamental concept in computer science. Unlike the Stack data structure learned previously, which follows the Last-In-First-Out (LIFO) principle, Queue works on the First-In-First-Out (FIFO) principle. A Queue is an ordered linear list of elements where insertions and deletions happen at different ends. Specifically, new elements are added at the rear (also called tail), and elements are removed from the front (also called head). This ordering ensures that the element which enters the queue first will be the first to be removed, maintaining fairness and order. Queues are widely observed in everyday life. For example, students standing in a queue for morning assembly, customers lining up at a bank's cash counter, or vehicles waiting in line at a petrol pump are all real-life examples of queues. These examples illustrate the FIFO principle where the first person or vehicle to join the queue is the first to be served or exit. Understanding Queue is essential as it forms the basis for many computer science applications, including task scheduling, resource management, and handling requests in servers. This section lays the foundation for learning the operations, implementations, and variations of the Queue data structure.
- Queue is a linear data structure following First-In-First-Out (FIFO) principle.
- Insertion happens at the rear (tail) and deletion at the front (head) of the queue.
- Queue differs from Stack which follows Last-In-First-Out (LIFO).
- Real-life examples include queues at banks, petrol pumps, and student assemblies.
- Queue ensures fairness by serving elements in the order they arrive.
- Queue is fundamental for various computer science applications.
- 📌 Queue: An ordered linear list following FIFO principle.
- 📌 FIFO (First-In-First-Out): The first element added is the first to be removed.
- 📌 Rear (Tail): End of the queue where new elements are added.
4.1.1 First In First Out (FIFO)
Concept4.1.1 First In First Out (FIFO)
This subsection elaborates on the core principle behind the Queue data structure called First In First Out (FIFO). FIFO means that the element that enters the queue first will be the first one to be removed. This principle is also known as First Come First Served (FCFS). In a queue, new elements are always added at one end called the REAR (or TAIL), and elements are removed from the opposite end called the FRONT (or HEAD). The FIFO principle ensures fairness and order in processing elements. For example, in a queue of people waiting to be served at a bank, the person who arrived first will be served first. Similarly, in computer science, this principle is used in scheduling tasks, handling requests, and managing resources. The section also highlights that the REAR and FRONT ends are fixed and distinct, which differentiates queue operations from other linear data structures like stacks where insertion and deletion happen at the same end.
- FIFO means first element added is first to be removed.
- Queue insertion happens at REAR (tail) and deletion at FRONT (head).
- FIFO is also known as First Come First Served (FCFS).
- REAR and FRONT are distinct ends of the queue.
- FIFO ensures fairness in serving elements.
- Queue differs from stack where both insertion and deletion happen at the same end.
- 📌 FIFO (First In First Out): Principle where first element added is first removed.
- 📌 REAR (Tail): End of queue where elements are inserted.
- 📌 FRONT (Head): End of queue where elements are removed.
4.1.2 Applications of Queue
Explanation4.1.2 Applications of Queue
This subsection discusses the numerous applications of the Queue data structure in both real life and computer science. In real life, queues are used to manage waiting lines and ensure fairness. For example, in railway ticket booking, if a ticket is
Practice Questions — Queue
Includes NCERT exercise questions with answers
Q1.1. Fill in the blank a) _______________ is a linear list of elements in which insertion and deletion takes place from different ends. b) Operations on a queue are performed in _______________ order. c) Insertion operation in a queue is called _______________ and deletion operation in a queue is called _______________. d) Deletion of elements is performed from _______________ end of the queue. e) Elements 'A', 'S', 'D' and 'F' are present in the queue, and they are deleted one at a time, _______________ is the sequence of element received. f) _______________ is a data structure where elements can be added or removed at either end, but not in the middle. g) A deque contains 'z', 'x', 'c', 'v' and 'b'. Elements received after deletion are 'z', 'b', 'v', 'x' and 'c'. _______________ is the sequence of deletion operation performed on deque.
Answer:
a) Queue is a linear list of elements in which insertion and deletion takes place from different ends. b) Operations on a queue are performed in FIFO (First In First Out) order. c) Insertion operation in a queue is called enqueue and deletion operation in a queue is called dequeue. d) Deletion of elements is performed from the front end of the queue. e) Elements 'A', 'S', 'D' and 'F' are present in the queue, and they are deleted one at a time, the sequence of elements received is A, S, D, F. f) Deque (Double Ended Queue) is a data structure where elements can be added or removed at either end, but not in the middle. g) A deque contains 'z', 'x', 'c', 'v' and 'b'. Elements received after deletion are 'z', 'b', 'v', 'x' and 'c'. The sequence of deletion operation performed on deque is: deleteFront(), deleteRear(), deleteFront(), deleteRear(), deleteFront().
Explanation:
a) Queue allows insertion at rear and deletion at front, hence different ends. b) Queue follows FIFO order, meaning first element inserted is first to be deleted. c) Enqueue means insertion at rear; dequeue means deletion from front. d) Deletion is always from the front end in a queue. e) Since queue is FIFO, elements are received in the order they were inserted. f) Deque allows insertion and deletion at both ends but not in the middle. g) The deletion sequence corresponds to alternating deletions from front and rear to produce the given output.
Q2.2. Compare and contrast queue with stack.
Answer:
Comparison between Queue and Stack: 1. Order of Operations: - Queue follows FIFO (First In First Out) order. - Stack follows LIFO (Last In First Out) order. 2. Insertion and Deletion: - In Queue, insertion is at rear and deletion is at front. - In Stack, insertion and deletion both happen at the top. 3. Usage: - Queue is used in scheduling, buffering, etc. - Stack is used in recursion, expression evaluation, backtracking. 4. Data Structure Type: - Queue is a linear data structure. - Stack is also a linear data structure but with different access rules. 5. Access: - Queue allows access only at front and rear. - Stack allows access only at top.
Explanation:
The key difference lies in the order in which elements are removed and the ends at which insertion and deletion happen. Queue is FIFO, Stack is LIFO. This affects their applications and behavior.
Q3.3. How does FIFO describe queue?
Answer:
FIFO stands for First In First Out. It means that the first element inserted into the queue will be the first one to be removed. This describes the behavior of a queue where elements are inserted at the rear and deleted from the front, ensuring that the order of processing is the same as the order of arrival.
Explanation:
FIFO ensures that elements are processed in the order they arrive, which is the fundamental property of a queue data structure.
Q4.4. Write a menu driven python program using queue, to implement movement of shuttlecock in it's box.
Answer:
Below is a sample Python program implementing a queue to simulate the movement of shuttlecock in its box: ```python from collections import deque class ShuttlecockBox: def __init__(self): self.queue = deque() def enqueue(self, item): self.queue.append(item) print(f"Inserted {item} into the box.") def dequeue(self): if len(self.queue) == 0: print("Box is empty. No shuttlecock to remove.") else: removed = self.queue.popleft() print(f"Removed {removed} from the box.") def display(self): if len(self.queue) == 0: print("Box is empty.") else: print("Current shuttlecocks in box:", list(self.queue)) def menu(): box = ShuttlecockBox() while True: print("\nMenu:") print("1. Insert shuttlecock") print("2. Remove shuttlecock") print("3. Display shuttlecocks") print("4. Exit") choice = input("Enter your choice: ") if choice == '1': item = input("Enter shuttlecock to insert: ") box.enqueue(item) elif choice == '2': box.dequeue() elif choice == '3': box.display() elif choice == '4': print("Exiting program.") break else: print("Invalid choice. Please try again.") if __name__ == '__main__': menu() ``` This program uses a queue to insert and remove shuttlecocks, simulating their movement in a box.
Explanation:
The program uses Python's deque to implement queue operations enqueue and dequeue. The menu allows user interaction to insert, remove, and display shuttlecocks, demonstrating queue behavior.
Q5.5. How is queue data type different from deque data type?
Answer:
Queue is a linear data structure where insertion happens at the rear end and deletion happens at the front end only. It follows FIFO order strictly. Deque (Double Ended Queue) is a generalized form of queue where insertion and deletion can happen at both front and rear ends. It supports more flexible operations than a queue.
Explanation:
Queue restricts operations to one end for insertion and the other for deletion, while deque allows both operations at both ends, making it more versatile.
Q6.6. Show the status of queue after each operation enqueue(34) enqueue(54) dequeue() enqueue(12) dequeue() enqueue(61) peek() dequeue() dequeue() dequeue() enqueue(1)
Answer:
Initial queue: empty Operation 1: enqueue(34) -> Queue: [34] Operation 2: enqueue(54) -> Queue: [34, 54] Operation 3: dequeue() -> removes 34 -> Queue: [54] Operation 4: enqueue(12) -> Queue: [54, 12] Operation 5: dequeue() -> removes 54 -> Queue: [12] Operation 6: enqueue(61) -> Queue: [12, 61] Operation 7: peek() -> returns 12 (front element), Queue unchanged: [12, 61] Operation 8: dequeue() -> removes 12 -> Queue: [61] Operation 9: dequeue() -> removes 61 -> Queue: [] Operation 10: dequeue() -> Queue is empty, cannot dequeue Operation 11: enqueue(1) -> Queue: [1]
Explanation:
Stepwise: - Enqueue adds element at rear. - Dequeue removes element from front. - Peek returns front element without removing. - When queue is empty, dequeue operation cannot remove any element. The queue status after each operation is updated accordingly.
Q7.7. Show the status of deque after each operation peek() insertFront(12) insertRear(67) deletionFront() insertRear(43) deletionRear() deletionFront() deletionRear()
Answer:
Initial deque: empty Operation 1: peek() -> Deque is empty, no element to peek. Operation 2: insertFront(12) -> Deque: [12] Operation 3: insertRear(67) -> Deque: [12, 67] Operation 4: deletionFront() -> removes 12 -> Deque: [67] Operation 5: insertRear(43) -> Deque: [67, 43] Operation 6: deletionRear() -> removes 43 -> Deque: [67] Operation 7: deletionFront() -> removes 67 -> Deque: [] Operation 8: deletionRear() -> Deque is empty, cannot delete
Explanation:
Operations on deque allow insertion and deletion at both ends: - insertFront adds element at front. - insertRear adds element at rear. - deletionFront removes element from front. - deletionRear removes element from rear. - peek returns front element if present. The deque status is updated after each operation accordingly.
Q8.8. Write a python program to check whether the given string is palindrome or not, using deque. (Hint: refer to algorithm 4.1)
Answer:
Below is a Python program to check palindrome using deque: ```python from collections import deque def is_palindrome(s): d = deque() for ch in s: d.append(ch) while len(d) > 1: front = d.popleft() rear = d.pop() if front != rear: return False return True # Example usage string = input("Enter a string: ") if is_palindrome(string): print(f"{string} is a palindrome.") else: print(f"{string} is not a palindrome.") ``` Explanation: - The string characters are added to a deque. - Characters are compared from front and rear by popping from both ends. - If all pairs match, the string is palindrome; otherwise not.
Explanation:
Using deque allows efficient removal of characters from both ends to compare for palindrome property. The algorithm continues until all characters are checked or a mismatch is found.
All 13 Chapters in Computer Science
Computer Science · Class 12