Results 1 to 3 of 3
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default linkedlist question

    class ChainNode { Object element; // an element of the list ChainNode next; // the next node in the chain
    ... // constructors (do not give codes!)
    } public class Chain implements LinearList {

    }
    protected ChainNode firstNode; // the first node in the chain protected int size; // number of nodes in the chain
    public Chain() {} // constructor

    public Object get (int index) { // returns the element at the specified index in this list; // (the first element in the list has index 0)
    ... // YOUR CODE REPLACES DOTS HERE (subquestion 3.a)
    }


    I'm not too sure on what to do for the get() method, could someone tell me if this is the correct way going about it.

    First get rid of the invalidindex exception.

    chainNode cursor = firstelem;

    for (int i=1 ; i <=index; i++){

    cursor = cursor.getNext();}

    return cursor;

  2. #2
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: linkedlist question

    Quote Originally Posted by stuckonjava View Post
    class ChainNode { Object element; // an element of the list ChainNode next; // the next node in the chain
    ... // constructors (do not give codes!)
    } public class Chain implements LinearList {

    }
    protected ChainNode firstNode; // the first node in the chain protected int size; // number of nodes in the chain
    public Chain() {} // constructor

    public Object get (int index) { // returns the element at the specified index in this list; // (the first element in the list has index 0)
    ... // YOUR CODE REPLACES DOTS HERE (subquestion 3.a)
    }


    I'm not too sure on what to do for the get() method, could someone tell me if this is the correct way going about it.

    First get rid of the invalidindex exception.

    chainNode cursor = firstelem;

    for (int i=1 ; i <=index; i++){

    cursor = cursor.getNext();}

    return cursor;
    a little confused so what is exactly your problem? It would be cursor = cursor.next;. Also change your for loop for i to be < not <=.. Are you trying to get the index or what is your problem here? And use code tags.

  3. #3
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: linkedlist question

    I think it is close. I think the '<=' will return the node at the index + 1. Also, what happens if you get an index that does not contain a node (negative or larger than size of list). I personally would test it with good and bad values and see what happens.

Similar Threads

  1. Question about LinkedList Iterator
    By kraigballa in forum New To Java
    Replies: 7
    Last Post: 02-10-2012, 06:10 PM
  2. Java newbie question on linkedlist index search
    By Mjall in forum New To Java
    Replies: 5
    Last Post: 03-25-2011, 11:54 PM
  3. I need help in LinkedList ...
    By Usman in forum Advanced Java
    Replies: 3
    Last Post: 03-25-2011, 02:38 AM
  4. Quick LinkedList question
    By mac in forum New To Java
    Replies: 7
    Last Post: 05-31-2010, 02:27 AM
  5. LinkedList help
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 09-19-2009, 08:24 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
  •