Trace a Queue (called Q) through the following operations: Queue Q = new Queue()
ID: 3673718 • Letter: T
Question
Trace a Queue (called Q) through the following operations:
Queue Q = new Queue();
Q.enqueue(new Integer(4));
Q.enqueue(new Integer(3));
//a) What is the content of the queue at this point?
Integer Y = Q.dequeue();
Q.enqueue(new Integer(7));
Q.enqueue(new Integer(2));
Q.enqueue(new Integer(5));
Q.enqueue(new Integer(9));
//b) What is the content of the queue at this point?
Integer Y = Q.dequeue();
Q.enqueue(new Integer(3));
Q.enqueue(new Integer(9));
//c) What is the content of the queue at this point?
Explanation / Answer
Queues: First In - First Out (FIFO)
· Enqueue()- store or add item to the queue
· Dequeue()- remove an item form the queue
In queue, dequeue data, pointed by front pointer and while enqueing data in queue we take help of rear pointer.
Enqueue Operation ():
Step 1 Check if queue is full.
Step 2 If a queue is full, produce overflow error and exit.
Step 3 If a queue is not full, increment rear pointer to point next empty space.
Step 4 Add data element to the queue location, where the rear is pointing.
Step 5 return success.
Initializing an Empty Queue: [null]
Front,rear | |, initially front and rear ill be same.
Enqueue 4 in the queue: | 4 | (head is 4)
Enqueue 3 in the queue: | 3 | 4 | (head is still 4)
Rear front
| 3 | 4|
Front=4, rear =3
Total elements in queue is 2 (3,4)
Dequeue Operation ()
Step 1 Check if the queue is empty.
Step 2 If the queue is empty, produce underflow error and exit.
Step 3 If the queue is not empty, access data where front is pointing.
Step 4 Increment front pointer to point next available data element.
Step 5 return success.
(b)
| 9 | 5 | 2 | 7 |
Rear =9 and front =7
The total elements in the queue 4 (7, 2, 5, 9)
9à5à2à7 (7 is dequeue from front side of the queue, next 2..)
(c)
| 9 | 3 |
Rear =9 and front=3
Total elements =2 (3, 9)
dequeue takes place from element 3.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.