Question

In: Computer Science

After the following statements execute, what are the contents of the deque? DequeInterface<String> myDeque = new...

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

Solutions

Expert Solution

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);


Related Solutions

What are the contents of the array after the for-loop in the following code?
(IN C)What are the contents of the array after the for-loop in the following code?int array[ SIZE ][ SIZE ] = { { 4, 5, 6, 7, 8 },{ 1, 2, 3, 4, 5 },{ 3, 6, 7, 8, 9 },{ 2, 3, 4, 5, 6 },{ 5, 6, 7, 8, 9 } };int i;int *ptr = array[ 0 ];for( i = 0; i < SIZE * SIZE; i++ ) {if( i % SIZE < 2 ) {*( ptr +...
As the following statements execute, what is the content of the priority queue? Show the content...
As the following statements execute, what is the content of the priority queue? Show the content of the queue after each step: PriorityQueue<String> myPriorityQueue = new PriorityQueue<>(); myPriorityQueue.offer("Jim"); myPriorityQueue.offer ("Jess"); myPriorityQueue.offer ("Jill"); myPriorityQueue.offer ("Jane"); String name = myPriorityQueue.poll(); myPriorityQueue.offer (name); myPriorityQueue.offer (myPriorityQueue.peek()); myPriorityQueue.offer ("Jim"); myPriorityQueue.poll();
Create a Deque class based on the following description of deque (double-ended queues). A dequeue is...
Create a Deque class based on the following description of deque (double-ended queues). A dequeue is a double-ended queue. You can insert items at either end and delete them from either end. Therefore, the deque offers two insert operations and two remove operations: insertLeft() and insertRight() removeLeft() and removeRight(). Deques may also have "peek" methods ( peekLeft() and peekRight() )that let you see the left or right item in the deque without remove the item from the deque. For this...
Write a method that accepts a String object as an argument and displays its contents backward....
Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display "ytivarg". Demonstrate the method in a program that asks the user to input a string and then prints out the result of passing the string into the method. Sample Run java BackwardString Enter·a·string:Hello·world↵ dlrow·olleH↵
what is the expected output of the following segment of code? dq = Deque() dq.push_back('M') dq.push_back('...
what is the expected output of the following segment of code? dq = Deque() dq.push_back('M') dq.push_back(' ') l = ['A', 'L', 'E', 'S', 'T', 'E'] for i in l: → if ord(i) % 3 == 0: → → dq.push_back(i) → elif ord(i) % 5 == 0: → → dq.push_front(i) → → dq.push_back(i) → elif ord(i) % 4 == 0: → → dq.push_back(i) → else: → → dq.peek_front() → → dq.push_front('X') → → dq.push_back('R') dq.push_front(dq.peek_back()) dq.pop_back() print(dq)
A deque is a data structure consisting of a list of items, on which the following...
A deque is a data structure consisting of a list of items, on which the following operations are possible: • push(x): Insert item x on the front end of the deque. • pop(): Remove the front item from the deque and return it. • inject(x): Insert item x on the rear end of the deque. • eject(): Remove the rear item from the deque and return it. Write routines to support the deque that take O(1) time per operation. In...
What are the values of a, b, c, d, and e after the following statements? int...
What are the values of a, b, c, d, and e after the following statements? int a = 5, b = 2, c = 8, d = 7, e = 4; int x = c - b + a; a = a + x - d; b = c * d - x; c = e + a / 2; d = x - c * a; e = x + d - c;
The release of the contents of cortical granules, ________. A. Occurs after a single sperm cell...
The release of the contents of cortical granules, ________. A. Occurs after a single sperm cell enters the egg. B. In some animal species, causes the jelly layer of the egg to physically change and lift away. C. Is the cause of a second reaction in which calcium ions sweep across the egg cell from the point of cell entry. D. Both A and B describe the release of the contents of cortical granules . E. All of the above...
1.   What is the output of the following code: string s1 = “X”; string s2 =...
1.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s2; 2.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s[0] + s[3]; 3.   What is the output of the following code: string s1 = “X”; string...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a =...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a = 20; 2 int b; 3 double x = 3.5; 4 String s = "All"; 5 char ch; 6 7 x += a; 8 x--; 9 a /= 4 - 1; 10 b = s.length(); 11 b += 4; 12 s += "is well"; 13 ch = s.charAt(b); 14 System.out.println("a = " + a + ", b = " + b); 15 System.out.println("x = "...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT