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( ); }
a. Define a Deque interface.
b. Define a LinkedDeque class (Node class)..
c. Define a Test class to test all methods
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:
The
Deque
interface defines the methods for a double-ended queue.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
).The
LinkedDeque
class implements theDeque
interface using a doubly linked list. It has ahead
, atail
, and asize
to maintain the state of the deque. The methods are implemented as follows:addFirst
andaddLast
add elements to the front and back of the deque, respectively.removeFirst
andremoveLast
remove elements from the front and back of the deque, respectively.isEmpty
checks if the deque is empty.first
andlast
return the elements at the front and back of the deque, respectively.
The
Main
class is used to test the functionality of theLinkedDeque
class. It creates aLinkedDeque
of integers, adds elements, and removes elements to demonstrate how the deque works.
Step by step
Solved in 5 steps with 2 images