0

我在不使用 java 泛型的情况下如何在 java 中的单链表中创建多个子列表时有些挣扎。我已经阅读了溢出中的多个问题,其中大多数都实现了泛型来解决问题。本质上,我想要一个结构,它可以创建一个包含 Dog、Cat 和 Snake 等值的链表,然后为每个值创建一个子列表,例如:

狗 ---> 贵宾犬,猎犬

|

v

猫 ---> 西伯利亚

|

v

蛇 ---> 眼镜蛇,蟒蛇

我相信我的主要问题在于我的 add 方法:

public void add(String topList, String botList)
    {
        head = new Node(topList, botList, head);
    }

其中 topList:[Dog, Cat, Snake] 和 botList:[Poodle,Retriever, Siberian, Cobra,Python]。

我觉得这个 addMethod 只是将我的头节点分支到两个不同的列表中,而不是实际上将我的 topList 与我的 botList 的元素链接起来。

我也试过:

public void add(String topList, String botList)
    {
        head = new Node(topList, head);
        head = new Node(botList, topList);
    }

但我很确定这不起作用,因为我的 void add 方法中有两个 String 变量。

所以我的问题是我将如何将我的 topList 元素与我的 botList 元素链接起来?任何帮助或参考将不胜感激。

这是我到目前为止所拥有的:

  import java.util.*;
  public class animal
  {
    Node head;
    public animal()
    {
        head = null;
    }
    public void add(String topList, String botList)
    {
        head = new Node(topList, botList, head);
    }
    public String toString()
    {
        String x;
        StringBuilder sb = new StringBuilder();
        Node p = head;
        while (p != null)
        {
            sb.append(p.topList + "\n " + p.botList +"\n");
            p = p.next;
        } 
        return new String (sb);
    }
    public static class Node
    {
        String topList;
        String botList;
        Node next;
        public Node (String t, String b, Node n)
        {
            topList = t;
            next = n;
            botList = b;
        }
    }
    public static void main(String args[])
    {
        animal list = new animal();
        list.add("Snake", "python");
        list.add("Dog", "poodle");
        list.add("Cat", "siberian");
        System.out.println(list);
    }
   } 

输出有点理想,但我知道我没有将两个列表链接在一起。此外,我只能向 botList 添加一个名称,并且我希望能够添加更多名称。

4

3 回答 3

1

我猜您在 Animal 类中需要以下方法:

public String get(String animal)
    {
        Node temp = head;
        while (temp!=null)
        {
            if (animal.equals(temp.topList))
            {
                return temp.botList;
            }
            temp = temp.next;
        }
        return null;
    }

在主体中,它看起来像这样:

public static void main(String args[])
{
    Animal list = new Animal();

    list.add("Snake", "python,Cobra");
    list.add("Dog", "poodle,Retriever");
    list.add("Cat", "siberian");
    System.out.println(list);
    System.out.println(list.get("Dog"));//shows poodle,Retriever

}
于 2013-02-24T21:30:25.760 回答
0

我可能会产生误解,但您似乎在以一种有点奇怪的方式构建它。您有两个列表:一个包含主要类型(狗、猫、蛇),另一个包含所有不同的子类型(贵宾犬、猎犬、西伯利亚、眼镜蛇、蟒蛇)。

相反,我认为第一个列表中的每个项目都应该链接到新列表,这意味着您总共有四个列表:

  1. 顶级列表:(狗,猫,蛇)
  2. 3 个子列表:(贵宾犬,猎犬),(西伯利亚),(眼镜蛇,Python)
于 2013-02-24T21:22:46.793 回答
0

像这样想。

单链表的内容仅包含数据和对链中下一个节点的引用。如果我们将第二个链表视为数据,那么我们可以将节点设计成这样。

  • 在顶层,我们创建的每个节点都是一些Animal(狗、猫和蛇)的节点。
  • 在数据层,我们插入的每一条数据都是一个链表,其中包含一些AnimalType关于它的信息。

你不会使用泛型,即使你这样做会更干净,但我确实设想这样的事情:

public class Animal {
    private String type;
    private AnimalType classification;

    // usual constructor and accessor
}

public class AnimalType {
    private String typeName;

    // usual constructor and accessor
}

public class AnimalNode {
    private Animal name;
    private AnimalTypeList data;
    private AnimalNode next;

    // usual constructor, accessor and mutator
}

public class AnimalList {  // AnimalList and AnimalTypeList are super similar
    private AnimalNode head;
    // operations and logic on list
}


public class AnimalTypeList {
    private AnimalType head;
    // operations and logic on list
}
于 2013-02-24T21:46:01.337 回答