Java programming assignment help

computer science

Description

import java.util.*;

public class LinkedListDeque<E> implements Deque<E> {

    
    public LinkedListDeque() {
        LinkedList<E> list = new LinkedList<E>();
    }
    
    public int size() { return list.size(); }
    
    public boolean isEmpty() { return list.size() == 0; }
    
    public void insertFirst(E e) { list.addFirst(e); }
    
    public void insertLast(E e) { list.addLast(e); }
    
    public E removeFirst() { return list.removeFirst(); }
    
    public E removeLast() { return list.removeLast(); }
    
    public E firstElement() { return list.getFirst(); }
    
    public E lastElement() { return list.getLast(); }
    
}
    
Can someone hep me format this properly? I can't figure it out / not sure if it is even remotely correct. I'm trying to 
/*
//nested node class
private static class Node {
private E element;
private Node prev;
private Node next;
public Node(E e, Node p, Node n) {
element = e;
prev = p;
next = n;
}
public E getElement() { return element; }
public Node getPrev() { return prev; }
public Node getNext() { return next; }
public void setPrev(Node p) { prev = p; }
public void setNext(Node n) { next = n; }
}
//end of node
//Linked list class
//instance variables
private Node head = null; //sentinels
private Node tail = null; //sentinels
private int size = 0; //# of elements
//constructor
public LinkedListDeque() {
head = new Node&lt;&gt;(null, null, null);
tail = new Node&lt;&gt;(null, null, null);
head.setNext(tail);

//access methods
//size and empty check
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
//get first or last elements
public E firstElement() {
if(isEmpty()) return null;
return head.getNext().getElement();
}
public E lastElement() {
if(isEmpty()) return null;
return tail.getPrev().getElement();
}
//update methods
//insert first or last
public void insertFirst(E e) {
addBetween(e, head, head.getNext());
}
public void insertLast(E e) {
addBetween(e, tail.getPrev(), tail);
}
//remove first or last
public E removeFirst() {
if(isEmpty()) return null;
return remove(head.getNext());
}
public E removeLast() {
if(isEmpty()) return null;
return remove(tail.getPrev());
}
//add and remove methods
private void addBetween(E e, Node before, Node after) {
Node newNode = new Node(e, before, after);
before.setNext(newNode);
after.setPrev(newNode);
size++;
}
private E remove(Node node) {
Node before = node.getPrev();
Node after = node.getNext();
before.setNext(after);
after.setPrev(before);
size--;
return node.getElement();
}
public int size();
public boolean isEmpty();
public void insertFirst(E element);
public void insertLast(E element);
public E removeFirst() throws EmptyStructureException;
public E removeLast() throws EmptyStructureException;
public E firstElement() throws EmptyStructureException;
public E lastElement() throws EmptyStructureException;
}
public interface Deque {
public int size();
public boolean isEmpty();
public void insertFirst(E element);
public void insertLast(E element);
public E removeFirst() throws EmptyStructureException;
public E removeLast() throws EmptyStructureException;
public E firstElement() throws EmptyStructureException;
public E lastElement() throws EmptyStructureException;
}
Put this interface into a file called Deque.java. Using this interface, then, write two deque implementations: one that uses an array and one that uses a linked list. These should go into files ArrayDeque.java and LinkedListDeque.java, respectively. For more information about interfaces in Java, please see the textbook.
In addition to implementing the methods in the Deque interface, you will need to define instance fields and create constructors and toString methods. The linked list deque is easy in that each method is either one line or error checking plus one line. For the Array, Deque looks at the curricular array implementation from the class notes. If you decide to use generics the constructor will need to use a cast operation as follows: elements = (E[]) new Object[DefaultSize]; where elements are the instance fields and E the generic type.
public class LinkedListDeque implements Deque {
LinkedListDeque() { ... }
...
}
public class ArrayDeque implements Deque {
ArrayDeque(int maxSize) { ... }
...
}
Thus, you need to create a class ArrayDeque, which implements the Deque interface using an array, and a class LinkedListDeque, which implements the Deque interface using a linked list. You may use classes from the Java utility package, but not any other libraries. Also, your ArrayDeque class should have a constructor, ArrayDeque(int maxSize), which creates an array with capacity cap, and your LinkedListDeque class should have a constructor, LinkedListDeque(), which initializes the linked list. The classes ArrayDeque and LinkedListDeque should then implement each of the methods defined in the Deque interface.
You'll need something to do in the case that a dequeue method is invoked when the deque is empty, or, in the case of the array implementation, when an insert method is invoked and the deque is full. Put (or download) the following exceptions in files EmptyStructureException.java and FullStructureException.java, respectively. Then you can throw new EmptyStructureException(), for example. See the textbook for more information about exceptions.
public class EmptyStructureException extends RuntimeException {
public EmptyStructureException() { super(); }
public EmptyStructureException(String s) { super(s); }
}
public class FullStructureException extends RuntimeException {
public FullStructureException() { super(); }
public FullStructureException(String s) { super(s); }
}
Now we can write simple adapter classes for our generic Deque interface to provide the stack and queue ADTs. The code for DequeStack already is filled in.
public class DequeStack implements Stack {
Deque deque;
DequeStack(Deque o) { deque = o; }
public int size() { return deque.size(); }
public boolean isEmpty() { return deque.isEmpty(); }
public Object top() { return deque.lastElement(); }
public Object pop() { return deque.removeLast(); }
public void push(Object o) { deque.insertLast(o); }
}
public class DequeQueue implements Queue {
...
public int size() { ... }
public boolean isEmpty() { ... }
public Object front() { ... }
public void enqueue(Object o) { ... }
public Object dequeue() { ... }
}
Thus we can instantiate an array-based stack of at most twelve elements with the code fragment
DequeStack stack = new DequeStack(new ArrayDeque(12));


Can I even do this the way I posted? I can't seem to use LinkedList methods. I've also tried to create the LinkedList code from scratch. Is there a way to easily do it as I originally posted or is this how it should be done? :


public interface Deque {


This is the question in its entirety but I'd really appreciate if you even just helped me with the first half / to get started.



Related Questions in computer science category