Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Below is an interface of a LinkedDeque, below that is a completed program for the interface. With that, I cannot use the same methods as
Below is an interface of a LinkedDeque, below that is a completed program for the interface. With that, I cannot use the same methods as that of the given program (it is from a textbook); I need a different approach to this LinkedDeque. Thank you!
_________________________________________________________________________________________________________________________________________
public interface DequeInterface{ /** Adds a new entry to the front/back of this dequeue. @param newEntry An object to be added. */ public void addToFront(T newEntry); public void addToBack(T newEntry); /** Removes and returns the front/back entry of this dequeue. @return The object at the front/back of the dequeue. @throws EmptyQueueException if the dequeue is empty before the operation. */ public T removeFront(); public T removeBack(); /** Retrieves the front/back entry of this dequeue. @return The object at the front/back of the dequeue. @throws EmptyQueueException if the dequeue is empty before the operation. */ public T getFront(); public T getBack(); /* Detects whether this dequeue is empty. @return True if the queue is empty, or false otherwise. */ public boolean isEmpty(); /* Removes all entries from this dequeue. */ public void clear(); }
_________________________________________________________________________________________________________________________________________
public class LinkedDequeimplements DequeInterface { private Node firstNode; private Node lastNode; //*************************************************************** public LinkedDeque() { firstNode = null; lastNode = null; } //*************************************************************** public void addToFront(T newEntry) { Node newNode = new Node(null, firstNode,lastNode); if(isEmpty()) lastNode = newNode; else firstNode.back = newNode; firstNode = newNode; } //*************************************************************** public void addToBack(T newEntry) { Node newNode = new Node(null, lastNode,null); if(isEmpty()) firstNode = newNode; else lastNode.next = newNode; lastNode = newNode; } //*************************************************************** public T removeFront() { T front = null; if(!isEmpty()){ front = firstNode.item; firstNode =firstNode.next; if(firstNode == null) lastNode = null; else firstNode.back = null; } return front; } //*************************************************************** public T removeBack() { T back = null; if(!isEmpty()){ back = lastNode.item; lastNode = lastNode.back; if(lastNode == null) firstNode = null; else lastNode.next = null; } return back; } //*************************************************************** public T getFront() { T front = null; if(!isEmpty()) front = firstNode.item; return front; } //*************************************************************** public T getBack() { T back = null; if(!isEmpty()) back = lastNode.item; return back; } //*************************************************************** public boolean isEmpty() { return (firstNode == null) && (lastNode == null); } //*************************************************************** public void clear() { firstNode = null; lastNode = null; } //*************************************************************** private class Node{ private T item; private Node next; private Node back; private Node(T item) { this(item, null,null); } private Node(T item, Node next,Node back) { this.item = item; this.next = next; this.back = back; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started