Question: **Java code** ** Add Entry class to test** public class FavoriteList { protected PositionList fList; // List of entries /** Constructor; O(1) time */ public

**Java code** ** Add Entry class to test**

public class FavoriteList { protected PositionList>fList; // List of entries /** Constructor; O(1) time */ public FavoriteList() { fList = new NodePositionList>(); }

//Complete your work and write out a main function to test. // Top, access and remove methods.

****** Please who can help with this question in Java programming language

This is the implementation of the NodePositionList & PositionList class

******NodePositionList

import java.util.Iterator;

public class NodePositionList implements PositionList {

protected int numElts; // Number of elements in the list protected DNode header, trailer; // Special sentinels

/** * Constructor that creates an empty list; O(1) time */ public NodePositionList() { numElts = 0; header = new DNode(null, null, null); // create header trailer = new DNode(header, null, null); // create trailer header.setNext(trailer); // make header and trailer point to each other } /** Checks if position is valid for this list and converts it to * DNode if it is valid; O(1) time */ protected DNode checkPosition(Position p) throws InvalidPositionException { if (p == null) throw new InvalidPositionException("Null position passed to NodeList"); if (p == header) throw new InvalidPositionException("The header node is not a valid position"); if (p == trailer) throw new InvalidPositionException("The trailer node is not a valid position"); try { DNode temp = (DNode) p; if ((temp.getPrev() == null) || (temp.getNext() == null)) throw new InvalidPositionException("Position does not belong to a valid NodeList"); return temp; } catch (ClassCastException e) { throw new InvalidPositionException("Position is of wrong type for this list"); } }

/** * Returns the number of elements in the list; O(1) time */ public int size() { return numElts; }

/** * Returns whether the list is empty; O(1) time */ public boolean isEmpty() { return (numElts == 0); }

/** * Returns the first position in the list; O(1) time */ public Position first() throws EmptyListException { if (isEmpty()) throw new EmptyListException("List is empty");

return header.getNext(); }

/** * Returns the first position in the list; O(1) time */ public Position last() throws EmptyListException { if (isEmpty()) throw new EmptyListException("List is empty");

return trailer.getPrev(); }

/** * Returns the position before the given one; O(1) time */ public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException { DNode v = checkPosition(p); DNode prev = v.getPrev(); if (prev == header) throw new BoundaryViolationException("Cannot advance past the beginning of the list");

return prev; }

/** * Returns the position after the given one; O(1) time */ public Position next(Position p) throws InvalidPositionException, BoundaryViolationException { DNode v = checkPosition(p); DNode next = v.getNext(); if (next == trailer) throw new BoundaryViolationException("Cannot advance past the end of the list");

return next; }

/** * Insert the given element before the given position, returning * the new position; O(1) time */ public void addBefore(Position p, E element) throws InvalidPositionException { DNode v = checkPosition(p); numElts++; DNode newNode = new DNode(v.getPrev(), v, element); v.getPrev().setNext(newNode); v.setPrev(newNode); }

/** * Insert the given element after the given position, returning * the new position; O(1) time */ public void addAfter(Position p, E element) throws InvalidPositionException { DNode v = checkPosition(p); numElts++; DNode newNode = new DNode(v, v.getNext(), element); v.getNext().setPrev(newNode); v.setNext(newNode); }

/** * Insert the given element at the beginning of the list, returning * the new position; O(1) time */ public void addFirst(E element) { numElts++; DNode newNode = new DNode(header, header.getNext(), element); header.getNext().setPrev(newNode); header.setNext(newNode); }

/** * Insert the given element at the beginning of the list, returning * the new position; O(1) time */ public void addLast(E element) { numElts++; DNode newNode = new DNode(trailer.getPrev(), trailer, element); trailer.getPrev().setNext(newNode); trailer.setPrev(newNode); }

/** * Remove the given position from the list; O(1) time */ public E remove(Position p) throws InvalidPositionException { DNode v = checkPosition(p); numElts--; DNode vPrev = v.getPrev(); DNode vNext = v.getNext(); vPrev.setNext(vNext); vNext.setPrev(vPrev); E vElem = v.element(); // unlink the position from the list and make it invalid v.setNext(null); v.setPrev(null);

return vElem; }

/** * Replace the element at the given position with the new element * and return the old element; O(1) time */ public E set(Position p, E element) throws InvalidPositionException { DNode v = checkPosition(p); E oldElt = v.element(); v.setElement(element);

return oldElt; }

/** * Returns a textual representation of a given node list */ public static String toString(PositionList l) { Iterator it = l.iterator(); String s = "["; while (it.hasNext()) { s += it.next(); // implicit cast of the next element to String if (it.hasNext()) s += ", "; }

return s + "]"; }

/** Returns an iterator of all the elements in the list. */ public Iterator iterator() { return new ElementIterator(this); }

/** * Returns an iterable collection of all the nodes in the list. */ public Iterable> positions() { // create a list of posiitons PositionList> P = new NodePositionList>(); if (!isEmpty()) { Position p = first(); while (true) { P.addLast(p); // add position p as the last element of list P if (p == last()) break; p = next(p); } } return P; // return P as our Iterable object } }

class EmptyListException extends RuntimeException { public EmptyListException(String err) { super(err); } }

***********PositionList

/** * An interface for a position list. */

public interface PositionList extends Iterable {

/** * Returns the number of elements in this list. */ public int size();

/** * Returns whether the list is empty. */ public boolean isEmpty();

/** * Returns the first node in the list. */ public Position first();

/** * Returns the last node in the list. */ public Position last();

/** * Returns the node after a given node in the list. */ public Position next(Position p) throws InvalidPositionException, BoundaryViolationException;

/** * Returns the node before a given node in the list. */ public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException;

/** * Inserts an element at the front of the list, returning new position. */ public void addFirst(E e);

/** * Inserts and element at the back of the list, returning new position. */ public void addLast(E e);

/** * Inserts an element after the given node in the list. */ public void addAfter(Position p, E e) throws InvalidPositionException;

/** * Inserts an element before the given node in the list. */ public void addBefore(Position p, E e) throws InvalidPositionException;

/** * Removes a node from the list, returning the element stored there. */ public E remove(Position p) throws InvalidPositionException;

/** * Replaces the element stored at the given node, returning old element. */ public E set(Position p, E e) throws InvalidPositionException;

/** * Returns an iterable collection of all the nodes in the list. */ public Iterable> positions();

}

class InvalidPositionException extends RuntimeException { public InvalidPositionException(String err) { super(err); } }

class BoundaryViolationException extends RuntimeException { public BoundaryViolationException(String err) { super(err); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!