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 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
A deque, short for "double-ended queue," is a data structure that allows you to insert and remove elements from both ends with O(1) time complexity. It combines the features of both stacks (where elements are added and removed from one end) and queues (where elements are added at one end and removed from the other end).
In a deque, you can perform the following operations:
1. `addFirst(E e)`: Adds an element to the front of the deque.
2. `addLast(E e)`: Adds an element to the back of the deque.
3. `removeFirst()`: Removes and returns the element at the front of the deque.
4. `removeLast()`: Removes and returns the element at the back of the deque.
5. `first()`: Returns the element at the front of the deque without removing it.
6. `last()`: Returns the element at the back of the deque without removing it.
7. `size()`: Returns the number of elements in the deque.
8. `isEmpty()`: Checks if the deque is empty.
Deques are useful in various situations where you need efficient access to elements at both ends of a collection, such as implementing algorithms involving sliding windows, reversing elements, or managing tasks with different priorities.
Step by step
Solved in 3 steps with 1 images