Staring out with Java (3rd Edition): Chapter 21 - Programming Challenge 1 *I am
ID: 3828665 • Letter: S
Question
Staring out with Java (3rd Edition): Chapter 21 - Programming Challenge 1
*I am being told that my question needs more information but I don't now what else to add?
Double-Ended Queue A deque (pronounced "deck is a list-based collection that allows additions and removals to take place at both ends. A deque supports the operations addFrontX), removeFront(), addRearP), remove Rear(), Size and empty(). Write a class that implements a deque that stores strings using a doubly-linked list that you code yourself. Demonstrate your class with a graphical user interface that allows users to manipulate the deque by typing appropriate commands in a JTextField component, and see the current state of the deque displayed in a JTextArea component. Consult the documentation for the JTextArea class for methods you can use to display each item in the deque on its own line.Explanation / Answer
import java.util.*;
public class DequeExample
{
public static void main(String[] args)
{
Deque deque = new LinkedList<>();
deque.add("Element 1 (Tail)");
deque.addFirst("Element 2 (Head)");
deque.addLast("Element 3 (Tail)");
deque.push("Element 4 (Head)");
deque.offer("Element 5 (Tail)");
deque.offerFirst("Element 6 (Head)");
deque.offerLast("Element 7 (Tail)");
System.out.println(deque + " ");
System.out.println("Standard Iterator");
Iterator iterator = deque.iterator();
while (iterator.hasNext())
System.out.println(" " + iterator.next());
Iterator reverse = deque.descendingIterator();
System.out.println("Reverse Iterator");
while (reverse.hasNext())
System.out.println(" " + reverse.next());
System.out.println("Peek " + deque.peek());
System.out.println("After peek: " + deque);
System.out.println("Pop " + deque.pop());
System.out.println("After pop: " + deque);
System.out.println("Contains element 3: " +
deque.contains("Element 3 (Tail)"));
deque.removeFirst();
deque.removeLast();
System.out.println("Deque after removing " +
"first and last: " + deque);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.