Question: Java Chapter 21 PC 1? part 2 Demonstrate your class with a graphical user interface that allows users to manipulate the deque by typing appropriate
Java Chapter 21 PC 1? part 2
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.
3rd source code for GUI that allows users to manipulate your deque GUIdeque Class
CODE
================
public class Deque
private Node
private Node
public void insertFront(T item){
//add element at the beginning of the queue
System.out.println("adding at front: "+item);
Node
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
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
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
if(tmpRear != null) tmpRear.setNext(null);
if(tmpRear == null) front = null;
System.out.println("removed from rear: "+rear.getValue());
rear = tmpRear;
}
public static void main(String a[]){
Deque
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();
}
}
class Node
private Node
private Node
private T value;
public Node
return prev;
}
public void setPrev(Node
this.prev = prev;
}
public Node
return next;
}
public void setNext(Node
this.next = next;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
