\"JAVA (Data structure) Stack and queque 7) What are the values for indexOfFront
ID: 3849217 • Letter: #
Question
"JAVA (Data structure) Stack and queque
7) What are the values for indexOfFront, indexOfRear and size of queue after the last operation is executed for a circular queue.
(you must provide three values)
ArrayQueue<String> myQ = new ArrayQueue<String>( 5);
array size is 5. (there are 5 slots in the array)
operation indexOfFront indexOfRear size of queue
myQ.enqueue(“one”);
myQ.enqueue(“two”);
myQ.enqueue(“three”);
myQ.dequeue( );
myQ.dequeue( );
myQ.dequeue( );
myQ.enqueue(“four”);
myQ.enqueue(“five”);
myQ.enqueue(“six”);
myQ.enqueue(“seven”);
myQ.dequeue( );
myQ.enqueue(“eight”);
myQ.dequeue( );
8) State why implementing a queue using a "circular array" is more
efficient that having the index of the front fixed at 0.
9) What is the principle difference in behavior between a stack and a queue?
a stack is used to reverse order of the data whereas a queue preserves orderExplanation / Answer
ANSWER:_
When enqueue is done then Indexof Rear is increamented as elements enter at the end of the queue
And When dequeue operation is done IndexofFront is incremented
If dequeue is done and then indexofFron and IndexofRear is set to -1
operation indexOfFront indexOfRear size of queue
myQ.enqueue(“one”); 0 0 1
myQ.enqueue(“two”); 0 1 2
myQ.enqueue(“three”); 0 2 3
myQ.dequeue( ); 1 2 2
myQ.dequeue( ); 2 2 1
myQ.dequeue( ); -1 -1 0
myQ.enqueue(“four”); 0 0 1
myQ.enqueue(“five”); 0 1 2
myQ.enqueue(“six”); 0 2 3
myQ.enqueue(“seven”); 0 3 4
myQ.dequeue( ); 1 3 3
myQ.enqueue(“eight”); 1 4 4
myQ.dequeue( ) 2 4 3 <----- Final answer
After all the operations are completed
IndexofFront = 2
IndexofRear = 4
Size of Queue = 3
Answer 2)
circular array is more efficient as traversal of elements of the queue becomes more efficient in circular queue than front index fixed to zero(normal array queue) as elements can be traversed from both the ends
Answer 3)
a stack is used to reverse order of the data whereas a queue preserves order
because stack follows LIFO -last In First Out , so the order becomes inverted and
queue follows FIFO -First in First Out , so the order is preserved
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.