Chapter 20 Programming Challenge 1
1. 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 addFront(x), removeFront(x), addRear(x), removeRear( ), 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 (GUI) 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 it's own line.
This must be Java.
The class names should be:
//Deque.java
public class Deque<T> {
private Node<T> front;
private Node<T> rear;
public void insertFront(T item){
//add element at the beginning of the queue
System.out.println("adding at front: "+item);
Node<T> nd = new Node<T>();
nd.setValue(item);
nd.setNext(front);
if(front != null) front.setPrev(nd);
if(front == null) rear = nd;
front = nd;
}
public void insertRear(T item){
//add element at the end of the queue
System.out.println("adding at rear: "+item);
Node<T> nd = new Node<T>();
nd.setValue(item);
nd.setPrev(rear);
if(rear != null) rear.setNext(nd);
if(rear == null) front = nd;
rear = nd;
}
public void removeFront(){
if(front == null){
System.out.println("Deque underflow!! unable to remove.");
return;
}
//remove an item from the beginning of the queue
Node<T> tmpFront = front.getNext();
if(tmpFront != null) tmpFront.setPrev(null);
if(tmpFront == null) rear = null;
System.out.println("removed from front: "+front.getValue());
front = tmpFront;
}
public void removeRear(){
if(rear == null){
System.out.println("Deque underflow!! unable to remove.");
return;
}
//remove an item from the beginning of the queue
Node<T> tmpRear = rear.getPrev();
if(tmpRear != null) tmpRear.setNext(null);
if(tmpRear == null) front = null;
System.out.println("removed from rear: "+rear.getValue());
rear = tmpRear;
}
}
class Node<T>{
private Node<T> prev;
private Node<T> next;
private T value;
public Node<T> getPrev() {
return prev;
}
public void setPrev(Node<T> prev) {
this.prev = prev;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
//Deque.java ends here
//Chp21PC01Demo.java
//Demo class that implements deque class
public class Chp21PC01Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Deque<Integer> deque = new Deque<Integer>();
deque.insertFront(34);
deque.insertFront(67);
deque.insertFront(29);
deque.insertFront(765);
deque.removeFront();
deque.removeFront();
deque.removeFront();
deque.insertRear(43);
deque.insertRear(83);
deque.insertRear(84);
deque.insertRear(546);
deque.insertRear(356);
deque.removeRear();
deque.removeRear();
deque.removeRear();
deque.removeRear();
deque.removeFront();
deque.removeFront();
deque.removeFront();
}
}
// Chp21PC01Demo.java ends here
Output:

Chapter 20 Programming Challenge 1 1. Double-Ended Queue A deque (pronounced "deck") is a list-based collection...