So here is my interface for my LinkedList:
package data_structures; import java.util.Iterator; import java.util.NoSuchElementException; public interface ListADT<E> extends Iterable<E> { // Adds the Object obj to the beginning of the list public void addFirst(E obj); // Adds the Object obj to the end of the list public void addLast(E o); // Removes the first Object in the list and returns it. // Returns null if the list is empty. public E removeFirst(); // Removes the last Object in the list and returns it. // Returns null if the list is empty. public E removeLast(); // Returns the first Object in the list, but does not remove it. // Returns null if the list is empty. public E peekFirst(); // Returns the last Object in the list, but does not remove it. // Returns null if the list is empty. public E peekLast(); // Removes the specific Object obj from the list, if it exists. // Returns true if the Object obj was found and removed, otherwise fa$ public boolean remove(E obj); // The list is returned to an empty state. public void makeEmpty(); // Returns true if the list contains the Object obj, otherwise false public boolean contains(E obj); // Returns true if the list is empty, otherwise false public boolean isEmpty(); // Returns true if the list is full, otherwise false public boolean isFull(); // Returns the number of Objects currently in the list. public int size(); // Returns an Iterator of the values in the list, presented in // the same order as the list. public Iterator<E> iterator(); }
I tried to compile my LinkedList but get the following error: data_structures/LinkedListDS.java:6: data_structures.LinkedListDS is not abstract and does not override abstract method iterator() in data_structures.ListADT
public class LinkedListDS<E> implements ListADT<E> {
Here is my LinkedList:
package data_structures; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedListDS<E> implements ListADT<E> { class Node<E> { E data; Node<E> next; public Node(E data) { this.data = data; next = null; } } private Node<E> head, tail; private int currentSize; public void LinkedListDS() { head=tail=null; currentSize= 0; } public void addFirst(E obj){ Node<E> newNode = new Node<E>(obj); if(head==null) head = tail= null; else { newNode.next=head; head = newNode; } currentSize=currentSize++; } public E removeFirst() { if (head==null) return null; E tmp=head.data; head =head.next; if (head==null) tail=null; currentSize--; return tmp; } }
Also i made a Stack code but get the following error when compiling: data_structures/Stack.java:7: '(' or '[' expected
list = new LinkedListDS<E>;
^
data_structures/Stack.java:17: '(' or '[' expected
}
^
package data_structures; public class Stack implements Iterable<E> { private ListADT<E> list; public Stack() { list = new LinkedListDS<E>; } public void push(E obj) { list.addFirst(obj); } public E pop() { return list.removeFirst(); } public Iterator<E> iterator() { return new IteratorHelper } class IteratorHelper implements Iterator<E> { Node<E> iteratorPointer; public IteratorHelper() { iteratorPointer = head; } public boolean hasNext() { return iteratorPointer != null; } public E next() { if (!hasNext()) throw NoSuchException(); E temp = iteratorPointer.data; iteratorPointer = iteratorPointer.next; return tmp; } public void remove() { throw new UnsupportedOperationException(); } } }
Any help would b greatly appreciated. I am new to programming and can't figure out what is wrong with my code.