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 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
Dequeinterface defines the methods for a double-ended queue.The
Nodeclass 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
LinkedDequeclass implements theDequeinterface using a doubly linked list. It has ahead, atail, and asizeto maintain the state of the deque. The methods are implemented as follows:addFirstandaddLastadd elements to the front and back of the deque, respectively.removeFirstandremoveLastremove elements from the front and back of the deque, respectively.isEmptychecks if the deque is empty.firstandlastreturn the elements at the front and back of the deque, respectively.
The
Mainclass is used to test the functionality of theLinkedDequeclass. It creates aLinkedDequeof integers, adds elements, and removes elements to demonstrate how the deque works.
Step by step
Solved in 5 steps with 2 images
