In: Computer Science
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports adding and removing at both the front and back. So, at a bare minimum, a deque has four operations: addFront(), removeFront(), addBack(), removeBack(). Suppose you have a deque D containing the numbers (1, 2, 3, 4, 5, 6, 7, 8), in this order. Suppose further that you have an initially empty queue Q. Give pseudo-code description of a method that uses only D and Q (and no other variables or objects) and results in D storing the elements (1, 2, 3, 5, 4, 6, 7, 8), in this order.
Simple pseudocode to make D = (1, 2, 3, 5, 4, 6, 7, 8)
Note: Please forgive if this is not in the required format, I’m just providing you step by step solution to implement this, then you can easily convert this into any format you like.
Initially Dequeue D contains: (1, 2, 3, 4, 5, 6, 7, 8)
Initially Queue Q contains: ()
Loop for 5 times, call removeFront() in D, enqueue the removed element to Q
D becomes (6,7,8)
Q becomes (1,2,3,4,5)
Loop for 3 times, call dequeue() in Q, pass the removed value to addBack() in D
D becomes (6,7,8,1,2,3)
Q becomes (4,5)
Loop for 2 times, Call dequeue() in Q, pass the removed value to addFront() in D
D becomes (5,4,6,7,8,1,2,3)
Q becomes ()
Loop for 3 times, Call removeBack() in D, pass the removed element to addFront() in D
D becomes (1,2,3,5,4,6,7,8)
Q becomes ()
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks