choose the right answer. explain please: If the string representation of a Queue
ID: 3868807 • Letter: C
Question
choose the right answer. explain please:
If the string representation of a Queue shows the first element as the leftmost character of the string and the last element as the rightmost element of the string, then what would the string representation of the Queue q be after the following sequence of operations?
Queue q = new Queue();
q.enqueue(A);
q.enqueue(B);
q.enqueue(C);
q.enqueue(q.dequeue());
q.enqueue(q.peek();
q.enqueue(D);
Note: peek() returns the first element of the queue (but does not remove it), enqueue(e) adds e to the queue, and dequeue() removes an element from the queue.
(a) BCAD
(b) ABCAD
(c) BCABD
Explanation / Answer
Ans) c.BCABD
Reason:
the method enqueue() will add the elements to the queue.
The method peek() will return the first element in the queue.
The method dequeue() will remove the first element in the queue.
Queue q = new Queue();
q.enqueue(A); //After adding, Then elements in the Queue is A
q.enqueue(B); //After adding, Then elements in the Queue is A B
q.enqueue(C); //After adding, Then elements in the Queue is A B C
q.enqueue(q.dequeue()); //After removing the first element in the queue and that same element is added to the queue , Then elements in the Queue is B C A
q.enqueue(q.peek(); //After returning the first element in the queue and that same element is added to the queue , Then elements in the Queue is B C A B
q.enqueue(D); //After Adding the element to the queue , Then elements in the Queue is B C A B D
_________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.