JAVA 1aSuppose you have a deque D containing the numbers 1 2

JAVA

1a)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 a initially empty queue Q. Write a method that uses only D and Q and no other variables and results in D storing the elements (1, 2, 3, 5, 4, 6, 7, 8) in this order.

b)Repeat the previous problem using the deque D and an initially empty stack S.

Solution

1a)

Deque D = new LinkedList<Integer>(), Q = new LinkedList<Integer>();

//initilize the deque

D.add(1);D.add(2);D.add(3);D.add(4);D.add(5);D.add(6);D.add(7);D.add(8);

//add first three elements

Q.add(D.pop());Q.add(D.pop());Q.add(D.pop());

//send 4 to the end

D.add(D.pop());

Q.add(D.pop());

// send 6,7,8 to the end

D.add(D.pop());D.add(D.pop());D.add(D.pop());

//now add the 4,6,7,8 to Q

Q.add(D.pop());Q.add(D.pop());Q.add(D.pop());Q.add(D.pop());

1b)

//initilize the D, as previously and define the stack

Stack S= new Stack();

//put 1,2,3

S.push(D.pop());S.push(D.pop());S.push(D.pop());

D.push(D.pop());

//put 5 into stack

S.push(D.pop());

//send 6,7,8 to the end

D.push(D.pop());D.push(D.pop());D.push(D.pop());

//now push 4,6,7,8 to stack

S.push(D.pop());S.push(D.pop());S.push(D.pop());S.push(D.pop());

JAVA 1a)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 a initially empty queue Q. Writ

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site