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

Question 110 pts What are the contents of the queue bankLine after the following statements execute?...
Question 110 pts What are the contents of the queue bankLine after the following statements execute? The front of the queue in the answers is the left-most value QueueInterface<String> bankLine = newLinkedQueue<>(); bankLine .enqueue(“John”); bankLine .enqueue(“Matthew”); String next = bankLine .dequeue(); bankLine .enqueue(“Drew”); bankLine .enqueue(“Heather”); bankLine .enqueue(“David”); next = bankLine .dequeue(); John, Drew, Heather Heather, John, Drew Drew, Heather, David David, Heather, Drew Flag this Question Question 210 pts What are the contents of the deque waitingLine after the following...
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...
1) What's the contents of array vals after the following code is executed? int[] vals =...
1) What's the contents of array vals after the following code is executed? int[] vals = {6, 1, 4, 5, 1}; int size = vals.length; for(int i = 0; i < size; i++) vals[i] = vals[i] * i; q) [ 6, 1, 4, 5, 1 ] b)[ 0, 1, 2, 3, 4] c)[0, 1, 8, 15, 4] d)[6, 2, 12, 20, 5] 2) Consider function foo defined below: static int[] foo(int vals[]) { int result[] = new int[vals.length - 1];...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT