【问题标题】:Storing more than 1 data item at a single index in a linked-list?在链表中的单个索引处存储超过 1 个数据项?
【发布时间】:2008-10-10 07:13:50
【问题描述】:

我试图在我的链表中的单个索引中存储超过 1 个数据项。我教科书中的所有示例似乎都说明了每个索引只添加一条数据。我假设可以添加更多?

例如,使用 Collections API 来存储整数,我会执行以下操作:

LinkedList <Integer>linky = new LinkedList<Integer>();
int num1 = 2, num2 = 22, num3 = 25, num4 = 1337;
linky.add(num1);

如何将 num2、num3 和 num4 添加到列表中的同一个第一个索引?多谢你们。

【问题讨论】:

    标签: java collections linked-list


    【解决方案1】:

    链表的工作方式似乎有点混乱。本质上,链表由节点组成,每个节点包含一个数据(一个对象,准确地说,它本身可以包含多个成员变量),以及指向链表中下一个节点的链接(如果存在则为空指针)是没有这样的下一个节点)。您还可以有一个双向链表,其中每个节点还具有指向列表中前一个节点的指针,以加快某些类型的访问模式。

    向单个节点添加多个“数据片段”听起来就像从一个节点添加多个链接,这会将您的链接列表变成 N 元

    要将多条数据添加到列表的末尾,以最常与链表关联的方式,只需执行以下操作:

    LinkedList <Integer>linky = new LinkedList<Integer>();
    int num1 = 2, num2 = 22, num3 = 25, num4 = 1337;
    linky.add(num1);
    linky.add(num2);
    linky.add(num3);
    linky.add(num4);
    

    或者,如果你希望链表的每个节点都有几条数据

    这些数据应该被打包到一个对象中(通过定义一个class将它们全部作为成员变量)。例如:

    class GroupOfFourInts
    {
       int myInt1;
       int myInt2;
       int myInt3;
       int myInt4;
    
       public GroupOfFourInts(int a, int b, int c, int d)
       {
         myInt1 = a; myInt2 = b; myInt3 = c; myInt4 = d;
       }
    }
    
    class someOtherClass
    {
    
      public static void main(String[] args)
      {
        LinkedList<GroupOfFourInts> linky = new LinkedList<GroupOfFourInts>();
        GroupOfFourInts group1 = new GroupOfFourInts(1,2,3,4);
        GroupOfFourInts group2 = new GroupOfFourInts(1337,7331,2345,6789);
        linky.add(group1);
        linky.add(group2);
      }
    }
    

    现在,linky 将有 2 个节点,每个节点将包含 4 个 ints、myInt1myInt2myInt3 , 和 myInt4

    注意

    以上都不是链表特有的。每当您想将一堆数据作为一个单元存储在一起时,都应该使用这种模式。您创建一个类,其中包含要存储在一起的每条数据的成员变量,然后创建该类型的任何 Java 集合类型(ArrayList、LinkedList、TreeList...)。

    确保您要使用链表(因为在选择 ArrayList 或 TreeList 时不会因编程难度而受到影响)。这将取决于您的数据访问模式。链表提供 O(1) 添加和删除,但 O(n) 查找,而 ArrayList 提供 O(1) 查找,但 O(n) 任意添加和删除。 TreeLists 提供 O(log n) 的插入、删除和查找。这些之间的权衡取决于您拥有的数据量以及您将如何修改和访问数据结构。

    当然,如果您的列表中只有

    希望这会有所帮助!

    【讨论】:

    • 哦,好吧,所以你是说我可以创建一个包含我需要的所有整数的对象,然后将该单个对象存储在节点中。我想尼尔森也是这么说的。
    • 哈哈,正如我评论的那样,你更新了它。非常感谢你的帮助!非常明确的答案。
    • 我不打算在列表中包含超过 25 个元素。我正在尝试将其实现到井字游戏 5x5 棋盘游戏中,以便能够撤消动作。 25是每场比赛的最大移动数,所以这应该不是问题。感谢您提供的大 O 符号信息,我们今天刚刚在课堂上介绍了它 :)
    • 好帖子;比我的更清楚:) 但我会提醒你:不要假设因为你的集合中元素很少,所以你使用哪种实现并不重要。如果您决定在游戏循环中使用 O(n) 查找将导致很大的损失,当随机访问是一个选项时:)
    • 同意,当您处于紧密循环中时,一切都很重要,但如果它不在程序的关键路径上,并且 O(1) 和 O(n) 之间的差异小于 1 阶数量级,通常不值得深思。一旦您的分析器告诉您其他情况,那么是时候明智地选择了:-)
    【解决方案2】:

    使用结构。

    例如:

    private struct Node
    {
        int Num1;
        int Num2;
        int Num3;
    }
    

    ...

    LinkedList<Node> list = new LnkedList<Node>();
    
    Node n = new Node();
    n.Num1 = 10;
    n.Num2 = 100;
    n.Num3 = 1000;
    list.Add(n);
    

    注意;我假设这是在 C# 中;如果我错了,请纠正我,我会修复代码;)

    如果您还没有在书中讨论过 OOP - 那么我建议您尝试一下;它将帮助您解决此类问题。

    【讨论】:

    • 你可以使用类代替结构~所以没问题!
    • 嗯......我从未接触过的少数主流语言之一......不过,我最终应该这样做。
    • 你不会发现太多不同之处,我一生中从未写过一行 C#,但我可以很好地阅读它;-)
    • 这种情况下如何遍历节点。你能告诉我..我正在实现类似的东西。
    【解决方案3】:

    为什么不这样:

    LinkedList<LinkedList<Integer>> linky = new LinkedList<LinkedList<Integer>>();
    //...
    linky.add(new LinkedList<Integer>().add( //...
    

    【讨论】:

      【解决方案4】:

      就像纳尔逊说你需要另一个对象,在 Java 中虽然你需要使用一个类。 如果您需要在您正在工作的类之外使用“节点”类,那么您需要将其设为公共类,并将其移动到它自己的文件中。

      private Class Node
      {
          //You might want to make these private, and make setters and getters
          public int Num1;
          public int Num2;
          puclic int Num3;
      }
      
      LinkedList<Node> list = new LinkedList<Node>();
      
      Node n = new Node();
      n.Num1 = 10;
      n.Num2 = 100;
      n.Num3 = 1000;
      list.Add(n);
      

      向 Nelson 道歉,因为他窃取了他的代码;)

      【讨论】:

      • 这种情况下如何遍历节点。你能告诉我..我正在实现类似的东西。
      【解决方案5】:

      这是一个完整的代码示例,展示了将结构添加到链表的用法:

      import java.util.LinkedList;
      class Node {
          int num1;
          int num2;
          int num3;
          int num4;
          public Node(int a, int b, int c, int d) {
              num1 = a; num2 = b; num3 = c; num4 = d;
          }
      }
      public class dummy {
          public static void main(String[] args) {
              LinkedList <Node>linky = new LinkedList<Node>();
              x myNode = new Node(2, 22, 25, 1337);
              linky.add(myNode);
          }
      }
      

      【讨论】:

        【解决方案6】:

        我真的不明白你想要达到什么目的,所以我建议解决另一个阅读问题的方法(在 java 中)。

        LinkedList <Integer>linky = new LinkedList<Integer>();
        linky.add(num1);
        
        // Lots of code possibly adding elements somewhere else in the list
        
        if (linky.size() > 0) { // Always good to be sure; especially if this is in another methode
         int first = linky.get(0);
         linky.set(0, first + num2);// Value of linky.get(0) is num1 + num2 
        }
        
        
        // The same again
        // Lots of code possibly adding elements somewhere else in the list
        
        if (linky.size() > 0) { // Always good to be sure; especially if this is in another methode
         int first = linky.get(0);
         linky.set(0, first + num3); // Value of linky.get(0) is num1 + num2 + num3
        }
        

        如果要添加的数字数量恒定(num1 .. num4),我个人碰巧最喜欢 Nelson 的解决方案, 如果它不是恒定的,我更喜欢 Gregor 的解决方案(使用 List 而不是 Node)。如果你在 java 中使用 Node 方法,我建议:

        // added static, Class to class
        private static class Node
        {
            //You might want to make these private, and make setters and getters
            public int Num1;
            public int Num2;
            puclic int Num3;
        }
        
        // Prefer interfaces if possible
        List<Node> list = new LinkedList<Node>();
        
        Node n = new Node();
        n.Num1 = 10;
        n.Num2 = 100;
        n.Num3 = 1000;
        list.add(n); // Add -> add
        

        很多吹毛求疵,但我认为如果可能的话,最好使用静态类而不是非静态私有类(通常应该是可能的)。

        【讨论】:

          猜你喜欢
          • 2015-02-07
          • 1970-01-01
          • 1970-01-01
          • 2016-08-23
          • 2017-07-05
          • 1970-01-01
          • 1970-01-01
          • 2012-07-21
          • 1970-01-01
          相关资源
          最近更新 更多