Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: cant get LinkedList to compile

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default cant get LinkedList to compile

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: cant get LinkedList to compile

    LinkedListDS is not abstract and does not override abstract method iterator()
    The compiler can't find a definition for the iterator() method in the LinkedListDS class.
    If you implement an interface you must define all of its methods.

    '(' or '[' expected
    Where is the ^ located in the error messages. The posting lost its position. Look at the location in the statement above the ^ for the problem. The compiler is expecting to see a '(' or a '[' there.
    Last edited by Norm; September 29th, 2012 at 04:44 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cant get LinkedList to compile

    Thanks for all your help:] I just have one more question. I am nearly done with my LinkedList here it is:
    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 void addLast(E obj){
                    // Create a new node.
                    Node < E > newNode = new Node < E > (obj);
            if(tail==null)
            tail=head=null;
            else {
                     tail.next = newNode; // Step 1
                    // Link the new node to the tail.
                    newNode.prev = tail; // Step 2
                    // The new node is the new tail.
                    tail = newNode; // Step 3
                    }
            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;
    }
     
    public E removeLast(){
            if (tail==null) return null;
            E tmp=tail.data;
            tail =  tail.prev; 
            if (tail==null) head=null;
            currentSize--;
    return tmp;
    }
     
    public E peekFirst(){
            if(head == null) return null;
            return head.data;
    }
     
    public E peekLast(){
            if(tail == null) return null;
            return tail.data;
    }       
     
    public boolean remove(E obj){
    if (head == null) return false;
    if (head.data.equals(obj)) {
    head = head.next;
    return true;
    currentSize--;
    }
     
    Node<E> current = head;
    while (current.next != null) {
    if (current.next.data.equals(obj)) {
    current.next = current.next.next;
    return true;
    currentSize--;   
    }
    current = current.next;
    }       
    return false;
     
    }
     
    public void makeEmpty(){
    head = tail = null;   
            currenSize = 0;
    }
    public boolean contains(E obj){
            Node<E> current = head;
            while (current != null) {   
                    if (current.data.equals(obj)) {
                            return true;
                                                    }
            current = current.next;
                                    }
            return false;
    }        
    public boolean isEmpty(){
    return first == null;
    }
     
    public boolean isFull(){
    return false;
    }
     
    public int size(){
            return currentSize;
     
    public Iterator<E> iterator() {
            return new IteratorHelper();
    }
     
    class IteratorHelper implements Iterator<E>{
            Node<E> IteratorPointer;
            public IteratorHelper(){
            IteratorPointer=head;
    }
     
    }
    }
    }

    I am getting the following errors when i compile:
    data_structures/LinkedListDS.java:122: illegal start of expression
    public Iterator<E> iterator() {
    ^
    data_structures/LinkedListDS.java:122: ';' expected
    public Iterator<E> iterator() {
    ^
    2 errors

    I have tried to change around the IteratorHelper class to see if that was the problem, but had no luck.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: cant get LinkedList to compile

    Check that all the {s have a matching } and that no methods are defined inside of other methods.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cant get LinkedList to compile

    I am having such a hard time getting my Iterator method to work. I know that I must include a class for the iterator and this is what I have so far:
    package data_structures;
     
     
    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 void addLast(E obj){
            Node<E> newNode = new Node<E>(obj);
            if(head == null){
                    head = newNode;
                    tail = newNode;
            currentSize++;
            return;
                            }
            tail.next = newNode;
            tail = newNode;
            currentSize++;
     
    }
     
    public E removeFirst() {
            if (head==null) return null;
            E tmp=head.data;
            if (head==null) return null;
            E tmp=head.data;
            head =head.next;
            if (head==null) tail=null;
            currentSize--; 
    return tmp;
    }
     
    public E removeLast(){
            Node<E> previous = null;
            Node<E> current = head;
            if(current == null) return null;
            while(current.next != null){
                    previous = current;
                    current = current.next;
                                    }
            if(previous == null) return removeFirst();
            previous.next = null;
            tail = previous;
            currentSize--;
            return current.data;
    }
     
     
    public E peekFirst(){
            if(head == null) return null;
            return head.data;
    }
     
    public E peekLast(){
            if(tail == null) return null;
            return tail.data;
    }
     
    public boolean remove(E obj){  
    if (head == null) return false;
    if (head.data.equals(obj)) {
    head = head.next;
    return true;
    }
     
    Node<E> current = head;
    while (current.next != null) {
    if (current.next.data.equals(obj)) {
    current.next = current.next.next;
    return true;
    }
    current = current.next;
    }
    currentSize--;
    return false;
     
    }
     
    public void makeEmpty(){
    head = tail = null;
            currentSize = 0;
    }
    public boolean contains(E obj){
            Node<E> current = head;
            while (current != null) {
                    if (current.data.equals(obj)) {
                            return true;
                                                    }
            current = current.next;
                                    }
            return false;
    }
    public boolean isEmpty(){
    return head == null; 
    }
     
    public boolean isFull(){
    return false;
    }
     
    public int size(){
            return currentSize;
    }
    public Iterator<E> iterator() {
            return new IteratorHelper();
    }
    class IteratorHelper implements Iterator<E>{
            Node<E> IteratorPointer;
            public IteratorHelper(){
            IteratorPointer=head;
    }
    }
    }

    And i get the following errors when I compile:
    data_structures/LinkedListDS.java:125: cannot find symbol
    symbol : class Iterator
    location: class data_structures.LinkedListDS<E>
    public Iterator<E> iterator() {
    ^
    data_structures/LinkedListDS.java:128: cannot find symbol
    symbol : class Iterator
    location: class data_structures.LinkedListDS<E>
    class IteratorHelper implements Iterator<E>{
    ^
    data_structures/LinkedListDS.java:128: interface expected here
    class IteratorHelper implements Iterator<E>{


    Can you please help me write the class for my iterator or point me in the right direction? I am relatively new to programming
    I simply need the iterator to return an Iterator of the values in the list, presented in the same order as the list.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: cant get LinkedList to compile

    cannot find symbol
    symbol : class Iterator
    You need an import statement so the compiler can find the definition of the Iterator interface.

    Start by making a skeleton class that implements the interface and has all the methods.
    Then work on them one at a time.

    You need to fix the formatting of the posted code. There should not be a column of }s. Each } should be in its own column underneath the start of the statement with the matching {
    Too many statements start in the first column. Unformated code is hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. is a Set the same as a Stack or LinkedList
    By mia_tech in forum Java Theory & Questions
    Replies: 1
    Last Post: July 9th, 2012, 05:50 PM
  2. [SOLVED] Sorting LinkedList
    By wdh in forum What's Wrong With My Code?
    Replies: 28
    Last Post: April 29th, 2012, 12:52 PM
  3. addInOrder LinkedList
    By PeskyToaster in forum Collections and Generics
    Replies: 1
    Last Post: April 6th, 2012, 06:16 AM
  4. LinkedList of String
    By oxnume in forum Collections and Generics
    Replies: 3
    Last Post: April 6th, 2012, 12:12 AM
  5. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM