title: Making a custom linked list class -- need help implementing get method
body: <p>I'm trying to make a custom linked list, so far I've figured out how to make the general structure and the two easiest methods (<code>insertFirst</code> and <code>deleteFirst</code>). The next thing I want to do is make a get method that takes the index of a link and then returns the string at that location. I don't have any index or address assigned to each link, so I don't see how to refer to a specific location in my linked list. I see that if I write <code>first.next</code> I get the second item, and <code>first.next.next</code> I get the third item.. But I need to figure out how to make my index parameter (the one passed into the get method) correlate with the proper location in my list. How can I do this?</p>
  <p>Here's my code:</p>
  <p><strong>Test code</strong>:</p>
  <pre><code> class LinkedListTest {
      public static void main(String[] args)
      {
          LinkedList list = new LinkedList();
          
          list.insertFirst(&quot;cat&quot;);
          list.insertFirst(&quot;dog&quot;);
          list.insertFirst(&quot;fish&quot;);
          list.insertFirst(&quot;cow&quot;);
          list.insertFirst(&quot;horse&quot;);
          list.insertFirst(&quot;pig&quot;);
          list.insertFirst(&quot;chicken&quot;);
          
          System.out.println(list.get(1));
          
      }
  }
  </code></pre>
  <p><strong>My class</strong>:</p>
  <pre><code>public class LinkedList
  {
      private Link first;
      
      
      public LinkedList()
      {
          first = null;
      }
      
      public void insertFirst(String word)
      {
          Link link = new Link(word);
          link.next = first;
          first = link;
      }
      
      public String deleteFirst()
      {
          Link temp = first;
          first = first.next;
          return temp.toString();
      }
      
      public String get(int index)
      {
          // the following is just to show that I can access different links
          // by adding more .next's after first--- but i need a way to access
          // the correct link based on the index passed in
          
          // String second = first.next.item;
          String third = first.next.next.item;
          // String fourth= first.next.next.next.item
          
          return third;
      }
      
  }
  
  
  
  public class Link
  {
      public String item;
      public Link next;
      
      
      //Link constructor
      public Link(String theItem)
      {
          item = theItem;
      }
  }
  </code></pre>