Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please finish the code as per the instructions in Java programming from the public boolean add(E e) question. import org.w3c.dom.Node; import java.util.Iterator; import java.util.NoSuchElementException; /**

Please finish the code as per the instructions in Java programming from the public boolean add(E e) question. import org.w3c.dom.Node;
import java.util.Iterator; import java.util.NoSuchElementException;

/** * This class implements an acyclic (non-cyclic), doubly-linked list. * @param  */
public class CiscDoublyLinkedList implements CiscList { /** * A reference to the first node in the list (or null if list is empty). */ private Node head; /** * A reference to the last node int the list (or null if list is empty). */
private Node tail; /** * Number of elements in the list. */ private int size;
/** * Returns the number of elements in this list. * * @return the number of elements in this list */ @Override public int size() { return size; }
/** * Returns {@code true} if this list contains no elements. * * * @return {@code true} if this list contains no elements */ @Override public boolean isEmpty() { if (size == 0){ return true; } return false; }
/** * Returns {@code true} if this list contains the specified element (compared using the {@code equals} method). * * * @param o element whose presence in this list is to be tested * @return {@code true} if this list contains the specified element * @throws NullPointerException if the specified element is null */ @Override public boolean contains(Object o) { if(o == null) { throw new NullPointerException(); } Node node = head; while(node != null) { if(node.data.equals(o)){ return true; } node = node.next; } return false; } 
/** * Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not * contain the element (compared using the {@code equals} method). * * * @param o element to search for * @return the index of the first occurrence of the specified element in this list, or -1 if this list does not * contain the element * @throws NullPointerException if the specified element is null */ @Override public int indexOf(Object o) { if (o == null){ throw new NullPointerException(); } for(int i =0; i 
* Removes all of the elements from this list. The list will be empty after this call returns. */ @Override public void clear() { head = null; tail = null; size = 0; } /** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException if the index is out of range */ @Override public E get(int index) { if (index<0 || index>= size){ throw new IndexOutOfBoundsException(); } Node current = head; for(int i =0; i 
/** * Appends the specified element to the end of this list. * * 

Lists may place the specified element at arbitrary locations if desired. In particular, an ordered list will * insert the specified element at its sorted location. List classes should clearly specify in their documentation * how elements will be added to the list if different from the default behavior (end of this list). * * @param e element to be appended to this list * @return {@code true} * @throws NullPointerException if the specified element is null */ @Override public boolean add(E e) { if(e == null){ throw new NullPointerException(); } return true; } /** * Replaces the element at the specified position in this list with the specified element. * * @param index index of the element to replace * @param element element to be stored at the specified position * @return the element previously at the specified position * @throws NullPointerException if the specified element is null * @throws IndexOutOfBoundsException if the index is out of range */ @Override public E set(int index, E element) { if (element == null){ throw new NullPointerException(); } if (index<0 || index>=size){ throw new IndexOutOfBoundsException(); } Node current = head; for(int i = 0; iThe returned array will be "safe" in that no references to it are maintained by this list. (In other words, * this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify * the returned array. * * @return an array containing all of the elements in this list in proper sequence */ @Override public Object[] toArray() { Object[] array = (Object[]) new Object[size]; int index = 0; for (Nodenode = head; node!=null;){ array[index] = node; index++; } return array; }

** * Removes the first occurrence of the specified element from this list, if it is present. If this list does not * contain the element, it is unchanged. Returns {@code true} if this list contained the specified element. * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element * @throws NullPointerException if the specified element is null */ @Override public boolean remove(Object o) { if (o == null){ throw new NullPointerException(); } return false; } /** * Removes the element at the specified position in this list. Shifts any subsequent elements to the left * (subtracts one from their indices). Returns the element that was removed from the list. * * @param index the index of the element to be removed * @return the element previously at the specified position * @throws IndexOutOfBoundsException if the index is out of range */ @Override public E remove(int index) { if (index<0 || index>=size){ throw new IndexOutOfBoundsException(); } return null; } /** * Inserts the specified element at the specified position in this list. Shifts the element * currently at that position (if any) and any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws NullPointerException if the specified element is null * @throws IndexOutOfBoundsException if the index is out of range */ @Override public void add(int index, E element) { if (element == null){ throw new NullPointerException(); } if (index<0 || index>= size){ throw new IndexOutOfBoundsException(); } } /** * Appends all elements in the specified list to the end of this list, in the order that they are returned by the * specified list's iterator. * * @param c list containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws NullPointerException if the specified list is null */ @Override public boolean addAll(CiscList c) { if (c == null){ throw new NullPointerException(); } return false; } /** * Returns an iterator over the elements in this list in proper sequence. * * @return an iterator over the elements in this list in proper sequence */ @Override public Iterator iterator() { return null; }
private static class CiscDoublyLinkedListIterator implements Iterator { /** * A reference to the node containing the next element to return. */ private Node nextNode; /** * Constructs an iterator beginning at the specified node. */ public CiscDoublyLinkedListIterator(Node head) { } /** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #next} would * return an element rather than throwing an exception.) * * @return {@code true} if the iteration has more elements */ @Override public boolean hasNext() { return true; } /** * Returns the next element in the iteration. * * @return the next element in the iteration * @throws NoSuchElementException if the iteration has no more elements */ @Override public E next() { if(!hasNext()) { throw new NoSuchElementException(); } return null; } } private static class Node { private E data; private Node next; private Node prev; private Node(E data, Node next, Node prev) { this.data = data; this.next = next; this.prev = prev; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Why does sin 2x + cos2x =1 ?

Answered: 1 week ago

Question

What are DNA and RNA and what is the difference between them?

Answered: 1 week ago

Question

Why do living creatures die? Can it be proved that they are reborn?

Answered: 1 week ago