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

private Node rear;

public void insertFront(T item){

//add element at the beginning of the queue

System.out.println("adding at front: "+item);

Node nd = new 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 = new 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 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 tmpRear = rear.getPrev();

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 = new 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 prev;

private Node next;

private T value;

public Node getPrev() {

return prev;

}

public void setPrev(Node prev) {

this.prev = prev;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!