Use a Doubly Linked List to implement a Deque a. Define a Deque interface. b. Define a LinkedDeque class (Node class).. c. Define a Test class to test all methods help me make a test class first code package Deque;   public class DLinkedList { private Node header = null; private Node trailer = null; private int size = 0;     public DLinkedList() { header = new Node<>(null, null,null); trailer = new Node<>(null,header,null); header.setNext(trailer); size = 0; }   public int getSize() { return size; } public boolean isEmpty() { return size ==0; }   public E getFirst(){ if(isEmpty()) { return null; } return header.getNext().getElement(); } public E getLast(){ if(isEmpty()) { return null; } return trailer.getPrev().getElement(); }   public void addFirst(E e) { addBetween(e,header,header.getNext()); }   public void addLast(E e) { addBetween(e,trailer.getPrev(), trailer); } public E removeFirst() { if (isEmpty()) { return null; } return remove(header.getNext( )); } public E removeLast() { if(isEmpty()) { return null; } return remove(trailer.getPrev()); } private void addBetween (E e, Node predecessor, Node successor) { Nodenewest = new Node<>(e , predecessor, successor); predecessor.setNext(newest); successor.setPrev(newest); size++; } private E remove(Node node) { Node predecessor = node.getPrev( ); Node successor = node.getNext( ); predecessor.setNext(successor); successor.setPrev(predecessor); size--; return node.getElement( ); } }     secound code   package Deque;   public interface Deque { int size( );   boolean isEmpty( );   E first( );   E last( );   void addFirst(E e);   void addLast(E e);   E removeFirst( );   E removeLast( );     }

icon
Related questions
Question
Use a Doubly Linked List to implement a Deque
a. Define a Deque interface.
b. Define a LinkedDeque class (Node class)..
c. Define a Test class to test all methods
help me make a test class
first code
package Deque;
 
public class DLinkedList<E> {
private Node<E> header = null;
private Node<E> trailer = null;
private int size = 0;
 
 
public DLinkedList() {
header = new Node<>(null, null,null);
trailer = new Node<>(null,header,null);
header.setNext(trailer);
size = 0;
}
 
public int getSize() {
return size;
}
public boolean isEmpty() {
return size ==0;
}
 
public E getFirst(){
if(isEmpty()) {
return null;
}
return header.getNext().getElement();
}
public E getLast(){
if(isEmpty()) {
return null;
}
return trailer.getPrev().getElement();
}
 
public void addFirst(E e) {
addBetween(e,header,header.getNext());
}
 
public void addLast(E e) {
addBetween(e,trailer.getPrev(), trailer);
}
public E removeFirst() {
if (isEmpty()) {
return null;
}
return remove(header.getNext( ));
}
public E removeLast() {
if(isEmpty()) {
return null;
}
return remove(trailer.getPrev());
}
private void addBetween (E e, Node<E> predecessor, Node<E> successor) {
Node<E>newest = new Node<>(e , predecessor, successor);
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}
private E remove(Node<E> node) {
Node<E> predecessor = node.getPrev( );
Node<E> successor = node.getNext( );
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getElement( );
}
}

 

 
secound code
 
package Deque;
 
public interface Deque<E> {
int size( );
 
boolean isEmpty( );
 
E first( );
 
E last( );
 
void addFirst(E e);
 
void addLast(E e);
 
E removeFirst( );
 
E removeLast( );
 
 
}
package Deque;
public class LinkedDeque<E> implements Deque<E> {
private DLinkedList<E> list = new DLinkedList<> ();
public LinkedDeque () {}
public int size () {
}
return list.getSize();
public boolean isEmpty ( ) {
return list.isEmpty();
}
public E first() {
}
return list.getFirst ();
}
public E last() {
return list.getLast ();
public void addFirst (E element) {
list.addFirst (element);
}
public void addLast (E element) {
list.addLast (element);
}
public E removeFirst()
{ return list.removeFirst(); }
public E removeLast() return list.removeLast (); }
}
Transcribed Image Text:package Deque; public class LinkedDeque<E> implements Deque<E> { private DLinkedList<E> list = new DLinkedList<> (); public LinkedDeque () {} public int size () { } return list.getSize(); public boolean isEmpty ( ) { return list.isEmpty(); } public E first() { } return list.getFirst (); } public E last() { return list.getLast (); public void addFirst (E element) { list.addFirst (element); } public void addLast (E element) { list.addLast (element); } public E removeFirst() { return list.removeFirst(); } public E removeLast() return list.removeLast (); } }
-
package Deque;
public class Node<E> {
private E element ;
private Node<E> next;
private Node<E> prev;
}
public Node (E e, Node<E> p, Node<E> n) {
element =e;
}
prev = p;
next = n;
public E getElement () { return element;}
public Node<E> getNext () {
return next;
}
public Node<E> getPrev () {
return prev;
}
public void setElement (E e) {
element =e;
}
}
public void setNext (Node<E> n) {
next = n;
}
public void setPrev (Node<E> p) {
prev= p;
Transcribed Image Text:- package Deque; public class Node<E> { private E element ; private Node<E> next; private Node<E> prev; } public Node (E e, Node<E> p, Node<E> n) { element =e; } prev = p; next = n; public E getElement () { return element;} public Node<E> getNext () { return next; } public Node<E> getPrev () { return prev; } public void setElement (E e) { element =e; } } public void setNext (Node<E> n) { next = n; } public void setPrev (Node<E> p) { prev= p;
Expert Solution
Step 1: Program Approach

The code you provided looks like a complete and correct implementation of a Deque using a doubly linked list in Java. Here's the breakdown of the provided code:

  1. The Deque interface defines the methods for a double-ended queue.

  2. The Node class represents nodes in the doubly linked list. Each node has an element, a reference to the previous node (prev), and a reference to the next node (next).

  3. The LinkedDeque class implements the Deque interface using a doubly linked list. It has a head, a tail, and a size to maintain the state of the deque. The methods are implemented as follows:

    • addFirst and addLast add elements to the front and back of the deque, respectively.
    • removeFirst and removeLast remove elements from the front and back of the deque, respectively.
    • isEmpty checks if the deque is empty.
    • first and last return the elements at the front and back of the deque, respectively.
  4. The Main class is used to test the functionality of the LinkedDeque class. It creates a LinkedDeque of integers, adds elements, and removes elements to demonstrate how the deque works.

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer