Results 1 to 6 of 6
  1. #1
    Mjall is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Java newbie question on linkedlist index search

    Hi guys,

    I am new here and signed up for the purpose of gaining and if I can distributing knowledge.
    I have a very specific question as to this piece of code I have developed,

    Java Code:
    public abstract class AbstractList<E> implements Iterable<E> {
    
        public int indexOf( E elem ) {
          /*This instance method is supposed to find the object E in this list and return its left most occurrence in the list. If the object does not exist within the list then return -1.  */
    
    
        }
    
    }
    Java Code:
    public interface Iterable<T> {
        public abstract Iterator<T> iterator();
    }
    Java Code:
    public interface Iterator<T> {
    
        public abstract boolean hasNext();
        public abstract T next();
    
    }
    This is my linked list class ...
    Java Code:
    import java.util.NoSuchElementException;
    
    public class LinkedList<E> extends AbstractList<E> {
    
        private static class Node<T> {
    
     private T value;
     private Node<T> next;
    
     private Node( T value, Node<T> next ) {
         this.value = value;
         this.next = next;
     }
        }
    
        private Node<E> first = null;
    
        private class LinkedListIterator implements Iterator<E> {
    
     private Node<E> current;
            
     public boolean hasNext() {
         return ( ( ( current == null ) && ( first != null ) ) || 
           ( ( current != null ) && ( current.next != null ) ) );
     }
    
     public E next() {
    
         if ( current == null ) {
      current = first;
         } else {
      current = current.next;
         }
         
         if ( current == null ) {
      throw new NoSuchElementException();
         }
    
         return current.value;
     }
    
        }
    
        public Iterator<E> Iterator() {
            return new LinkedListIterator();
        }
    
        public int size() {
    
            Node<E> p = first;
            int count = 0;
    
            while ( p != null ) {
                p = p.next;
                count++;
            }
    
            return count;
        }
    
    
        public boolean addFirst( E e) {
    
     boolean added = false;
    
            if ( e != null ) {
    
         first = new Node<E>( e, first );
         added = true;
    
     }
    
            return added;
        }
    
    }
    Any help at all will be highly appreciated and I will remain forever thankful!

    Mjall

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Help with what? Ask a specific question and get a specific answer.

  3. #3
    Mjall is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    True, I lost focus my bad.

    The question is
    I would like to in the AbstractFinal class, be able to implement the indexOf() method.
    The description for the method is that it receives indexOf(E elem) and what the method is supposed to return is the index of the left most occurrence of the object elem in the linkedlist.
    If I can clarify more please let me know.

    Thanks

    Mjall

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Still no question. All you have done is restate your requirements. "How do I write the indexOf method?" is a question but not specific. Make an attempt at writing the code, then post your attempt here and explain what is wrong with it (errors, incorrect behaviour). Once again a specific question will get you a specific answer.

  5. #5
    Mjall is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Ok so what I did was


    Java Code:
    public abstract class AbstractList<E> implements Iterable<E> {
    
        public int indexOf( E elem ) {
          /*This instance method is supposed to find the object E in this list and return its left most occurrence in the list. If the object does not exist within the list then return -1.  */
    int counter = 0;
    while(this.next != elem)   //this.next = the info part of the node in front "this"
    counter++;
        }
    return counter;
    
    }
    What I mean to accomplish is basically to keep my pointer at node1 and check node2 for elem. If elem doesn't exist in node2 then counter goes up and until I find elem in a node counter++

    Now the error I get is that next is not a value specified and is not defined for abstract.

    So my question is, How can I keep my pointer at the beginning node while checking the next node in the list for elem and returning the index of the node where elem is found.
    I can handle the -1 return I expect with a simple if statement.

    Thanks

    Mjall

  6. #6
    Mjall is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 01-13-2011, 10:58 AM
  2. How to search russian texts in Lucene index?
    By ArtUrlWWW in forum Lucene
    Replies: 0
    Last Post: 01-11-2011, 11:46 AM
  3. Problems with javadoc Index search.
    By heven in forum NetBeans
    Replies: 0
    Last Post: 08-09-2010, 10:46 AM
  4. problem with index file search
    By rencin in forum Lucene
    Replies: 4
    Last Post: 08-17-2009, 10:27 AM
  5. Newbie search array question
    By CirKuT in forum New To Java
    Replies: 19
    Last Post: 09-14-2008, 06:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •