In: Computer Science
After the following statements execute, what are the contents of the deque?
DequeInterface<String> myDeque = new
LinkedDeque<>(); myDeque.addToFront("Jim");
myDeque.addToFront("Jess");
myDeque.addToBack("Jill");
myDeque.addToBack("Jane");
String name = myDeque.removeFront();
myDeque.addToBack(name);
myDeque.addToBack(myDeque.getFront());
myDeque.addToFront(myDeque.removeBack());
myDeque.addToFront(myDeque.getBack());
I need to get this as an output:
Jim
Jess Jim
Jess Jim Jill
Jess Jim Jill Jane
Jim Jill Jane
Jim Jill Jane Jess
Jim Jill Jane Jess Jim
Jim Jim Jill Jane Jess
Jess Jim Jim Jane Jess
Help me please
Here is the solution to above problem . Please read the explanation for more information
//create the dequeue
DequeInterface<String> myDeque = new
LinkedDeque<>();
myDeque.addToFront("Jim");
System.out.println(myDeque);
myDeque.addToFront("Jess");
System.out.println(myDeque);
//add jim to dequeue
myDeque.addToBack("Jill");
System.out.println(myDeque);
//add back Jane
myDeque.addToBack("Jane");
System.out.println(myDeque);
//remove Jess wich is at front
//Jim Jill Jane
String name = myDeque.removeFront();
System.out.println(myDeque);
//add Jess back to Deque
myDeque.addToBack(name);
System.out.println(myDeque);
//this will add jim to back
myDeque.addToBack(myDeque.getFront());
System.out.println(myDeque);
//this will add Jim to front
myDeque.addToFront(myDeque.removeBack());
System.out.println(myDeque);
//you need to remove jil from the list for that
String name = myDeque.removeFront();
String name2=myDeque.removeFront();
//remove jill
myDeque.removeFront();
//add elements back
myDeque.addToFront(name);
myDeque.addToFront(name2);
//add jess to front
myDeque.addToFront(myDeque.getBack());
System.out.println(myDeque);