我正在尝试制作一个自定义链表,到目前为止,我已经弄清楚了如何制作一般结构和两种最简单的方法(insertFirst 和 deleteFirst)。我想做的下一件事是创建一个获取链接索引的 get 方法,然后返回该位置的字符串。我没有为每个链接分配任何索引或地址,因此我看不到如何引用链接列表中的特定位置。我看到如果我写 first.next 我得到第二项,first.next.next 我得到第三项....但我需要弄清楚如何制作我的索引参数(传递给 get 方法的那个) 与我列表中的正确位置相关....我该怎么做?
请原谅任何草率的代码...在掌握链表结构后,我一定会清理细节。
这是我的代码,在此先感谢!
测试代码
 class LinkedListTest {
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();
        list.insertFirst("cat");
        list.insertFirst("dog");
        list.insertFirst("fish");
        list.insertFirst("cow");
        list.insertFirst("horse");
        list.insertFirst("pig");
        list.insertFirst("chicken");
        System.out.println(list.get(1));
    }
}
我的课
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;
    }
}