Download Introduction to queue in python and more Study Guides, Projects, Research Computer science in PDF only on Docsity!
QUEUE
2 MARKS QUESTIONS
Q1. What is meant by the FIFO principle in the context of queues? Explain with an example. Answer: FIFO stands for First-In-First-Out , meaning the element that enters the queue first is the one that exits first. Example: In a bank queue, the first customer to enter the line is the first to be served at the counter. Q2. Differentiate between Queue and Stack. Answer: Queue Stack Follows FIFO (First In First Out) Follows LIFO (Last In First Out) Insertion at rear, deletion at front Insertion and deletion at same end Q3. What is underflow and when does it occur in a queue? Answer: Underflow is an error that occurs when a dequeue operation is attempted on an empty queue , i.e., when there are no elements to remove. Q4. Name any four operations supported by a queue. Answer:
QUEUE
The four operations are:
- enqueue() – to insert an element
- dequeue() – to delete an element
- peek() – to view the front element
- isEmpty() – to check if the queue is empty Q5. Write the syntax of a function to insert an element at the rear of a queue in Python. Answer: def enqueue(myQueue, element): myQueue.append(element) Here, append() adds the element at the end (rear) of the list used as a queue. Q6. Write a Python function to check whether a queue is empty or not. Answer: def isEmpty(myQueue): if len(myQueue) == 0 : return True else : return False Q7. What is a deque? Mention one of its applications. Answer: A deque (double-ended queue) is a linear data structure that allows insertion and deletion from both front and rear ends. Application: Checking whether a string is a palindrome using character comparisons from both ends. Q8. Give the syntax of the deletion from rear operation in a deque using Python. Answer: def deletionRear(myDeque): if not isEmpty(myDeque): return myDeque.pop() else : print("Deque empty") Q9. Distinguish between insertFront() and insertRear() in deque. Answer:
QUEUE
def enqueue(myQueue, element): myQueue.append(element)
- dequeue() removes and returns the front element: def dequeue(myQueue): if not isEmpty(myQueue): return myQueue.pop( 0 ) else : print("Queue is empty")
- peek() returns the front element without removing it: def peek(myQueue): if isEmpty(myQueue): print("Queue is empty") return None else : return myQueue[ 0 ] Q3. Describe any three real-life or computer science applications of queues. Answer:
- Customer Care Calls: Incoming calls are put in a queue and served in FIFO order.
- Print Queue: Multiple print requests from different users are queued and printed one by one.
- Operating System Task Scheduling: In multitasking systems, jobs are queued for processor time in FIFO order. Q4. What is a deque? Explain with two relevant operations and their Python implementations. Answer: A deque (double-ended queue) allows insertion and deletion from both front and rear.
- insertFront(): def insertFront(myDeque, element): myDeque.insert( 0 , element)
- deletionRear(): def deletionRear(myDeque): if not isEmpty(myDeque): return myDeque.pop() else : print("Deque empty")
QUEUE
Q5. Compare queue and deque based on structure and operations. Answer: Aspect Queue Deque Structure Linear structure with two ends Linear, double-ended structure Insertion At rear only At both front and rear Deletion From front only From both front and rear Flexibility Less flexible More flexible, supports stack and queue behavior Q6. Write the complete Python function to check if a queue is empty and to return its size. Answer:
- isEmpty(): def isEmpty(myQueue): if len(myQueue) == 0 : return True else : return False
- size(): def size(myQueue): return len(myQueue) These functions help prevent underflow and allow queue size tracking. Q7. Describe the steps of palindrome checking using deque. Answer:
- Traverse the string character by character.
- Insert each character into the deque from the rear.
- Remove one character from the front and one from the rear , and compare.
- Repeat until all characters are compared or only one remains.
- If all pairs match, the string is a palindrome ; otherwise, it’s not. Q8. What are the roles of getFront() and getRear() in a deque? Provide their syntax. Answer:
- getFront(): Returns the front element without deleting it.
QUEUE
Return len(myQueue) == 0 def dequeue(myQueue): if not isEmpty(myQueue): return myQueue.pop( 0 ) else : print("Queue is empty") def size(myQueue): return len(myQueue) def peek(myQueue): if isEmpty(myQueue): print("Queue is empty") return None else : return myQueue[ 0 ] These functions simulate a queue using Python’s list type. Q3. Compare queue and deque data structures in detail. Mention three applications where deque is more suitable than queue. Answer: Feature Queue Deque (Double-Ended Queue) Insertion Only at rear At both front and rear Deletion Only from front From both front and rear Flexibility Less flexible More flexible Use case Print queue, OS job scheduler Palindrome checking, undo-redo Applications where deque is more suitable:
- Palindrome checking – allows character comparison from both ends.
- Undo/Redo in editors – requires stack-like behavior.
- Browser tab history – older entries are deleted from the rear, new ones from the front. Q4. Describe the algorithm and steps to check if a string is a palindrome using deque. Provide a brief explanation with an example. Answer: Algorithm Steps:
QUEUE
- Traverse the string from left to right.
- Insert each character into deque using insertRear.
- Remove one character from front and rear , and compare them.
- Repeat the process until:
- Deque is empty → string is palindrome.
- Characters don’t match → string is not palindrome. Example: For string "madam":
- Insert: m, a, d, a, m - Compare m with m, then a with a, and only d remains → palindrome. Q5. Write a menu-driven Python program that performs basic operations on a deque. Include insertion and deletion from both ends and necessary checks. Answer: def insertFront(myDeque, element): myDeque.insert( 0 , element) def insertRear(myDeque, element): myDeque.append(element) def deletionFront(myDeque): if not isEmpty(myDeque): return myDeque.pop( 0 ) else : print("Queue underflow") def deletionRear(myDeque): if not isEmpty(myDeque): return myDeque.pop() else : print("Deque empty") def isEmpty(myDeque): return len(myDeque) == 0 def main(): myDeque = [] choice = int(input("Enter 1 for queue mode, 2 for deque mode: ")) if choice == 1 : insertRear(myDeque, input("Insert at
STUDENT NOTES || CHAPTER 4
QUEUE
April 18, 2025 print("Deleted:", deletionFront(myDeque)) else : insertFront(myDeque, input("Insert at front: ")) print("Rear element:", myDeque[- 1 ]) print("Deleted from rear:", deletionRear(myDeque)) This menu-driven program allows use of deque as either a normal queue or double-ended queue. Q6. Explain with examples how queues are used in real life and in computer science applications. Answer: Real-life applications:
- Bank queues: First customer is served first.
- Toll booths: Vehicles are allowed in FIFO order.
- Customer care calls (IVRS): Callers wait in order of arrival. Computer science applications:
- Print queue: Print jobs are handled one by one in order.
- Web server: Handles requests in FIFO order.
- Job scheduling in OS: Jobs wait in queue for processor time. Q7. What are the major operations supported by deque? Write the syntax of any four operations with explanation. Answer: Operations:
- insertFront(): myDeque.insert( 0 , element) Inserts element at the front.
- insertRear(): myDeque.append(elem ent) Inserts element at the rear.
- deletionFront(): myDeque.pop( 0 )
STUDENT NOTES || CHAPTER 4
QUEUE
April 18, 2025 Deletes element from front.
- deletionRear():
QUEUE
for item in myQueue: print(item)
- peek(): Returns only the front element without deleting.
- displayQueue(): Prints all elements of the queue without modifying it. Q10. Explain the following terms with reference to queues: (a) Underflow (b) Overflow (c) Rear (d) Front (e) isEmpty() Answer: a) Underflow : Attempting to delete from an empty queue. b) Overflow : Attempting to insert into a full queue (not applicable in Python lists). c) Rear : End where new elements are inserted. d) Front : End from where elements are removed. e) isEmpty() : A function that checks if a queue has no elements (len(queue) == 0 ). Q11. Explain any five applications of deque from real life and computer science contexts. Answer: A deque (double-ended queue) is a linear data structure that allows insertion and deletion at both ends —front and rear. Due to this flexibility, deque is used in various real-life and computer science scenarios:
- Re-entry in Ticket Queue (Real-life): A person who has already purchased a train ticket may return to the counter for a query and be allowed to rejoin from the front of the queue instead of the rear. This is possible only with a deque structure.
- Toll Booth Management (Real-life): At highway toll plazas, if one booth becomes free, vehicles from the rear of other queues may shift to the front of the available booth’s queue , involving both front and rear deletions/insertions— ideal for deque.
- Palindrome Checking (Computer Science): Characters of a string are inserted into a deque from the rear. Then, characters are compared and removed from both front and rear to check if the string is a palindrome.
- Undo/Redo Functionality in Editors: The undo/redo feature in text editors uses deque, where operations can be pushed and popped from
QUEUE
either end, simulating both stack and queue behavior.
- Browser Tab History Navigation: URLs visited in a browser are stored in a deque. The most recent URL is reopened first (LIFO), and the oldest can be discarded from the rear (FIFO), based on memory limits. Thus, deque is a versatile data structure capable of handling complex real-world and programming sce- narios efficiently.
CHAPTER END EXERCISES
1. Fill in the blanks a) is a linear list of elements in which insertion and deletion takes place from different ends. Answer: Queue b) Operations on a queue are performed in order. Answer: FIFO (First In First Out) c) Insertion operation in a queue is called and deletion operation in a queue is called . Answer: enqueue, dequeue d) Deletion of elements is performed from end of the queue. Answer: front 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. Answer: A, S, D, F f) is a data structure where elements can be added or removed at either end, but not in the middle. Answer: Deque 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: deletionFront(), deletionRear(), deletionRear(), deletionFront(), deletionFront() 2. Compare and contrast queue with stack. Answer:
QUEUE
print("Removed:", dequeue(box)) elif choice == 3 : bre ak else : print("Invalid choice")
5. How is queue data type different from deque data type? Answer: Queue | Deque | ||–| | Insertion only at rear | Insertion at front and rear | | Deletion only from front | Deletion from front and rear | | Less flexible | More flexible | | Cannot simulate stack behavior | Can simulate both stack and queue | 6. Show the status of queue after each operation: Operations: enqueue(34) enqueue(54) dequeue() enqueue(12) dequeue() enqueue(61) peek() dequeue() dequeue() dequeue() dequeue() enqueue(1) Answer (Step-by-step queue status): 1. enqueue(34) → [34] 2. enqueue(54) → [34, 54] 3. dequeue() → [54] 4. enqueue(12) → [54, 12]
QUEUE
- dequeue() → [12]
- enqueue(61) → [12, 61]
- peek() → 12
- dequeue() → [61]
- dequeue() → []
- dequeue() → Underflow (queue is empty)
- dequeue() → Underflow
- enqueue(1) → [1] 7. Show the status of deque after each operation: Operations: peek() insertFront(12) insertRear(67) deletionFront() insertRear(43) deletionRear() deletionFront() deletionRear() Answer (Step-by-step deque status):
- peek() → Deque is empty
- insertFront(12) → [12]
- insertRear(67) → [12, 67]
- deletionFront() → [67]