简体   繁体   English

在链表的单个索引中存储 1 个以上的数据项?

[英]Storing more than 1 data item at a single index in a linked-list?

I am trying to store more than 1 data item at a single index in my linked-list.我试图在我的链表中的单个索引中存储 1 个以上的数据项。 All of the examples in my textbook seem to illustrate adding only 1 piece of data per index.我教科书中的所有示例似乎都说明了每个索引仅添加 1 条数据。 I'm assuming it is possible to add more?我假设可以添加更多?

For example, using the Collections API to store an integer I would do the following:例如,使用 Collections API 存储一个整数,我将执行以下操作:

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

How would I go about adding num2, num3, and num4 to the same first index in the list?我将如何将 num2、num3 和 num4 添加到列表中相同的第一个索引? Thanks guys.谢谢各位。

There seems to be a little confusion about how linked lists work.关于链表的工作方式似乎有些混乱。 Essentially, a linked list is composed of nodes, each of which contains one datum (an object, which itself can contain several member variables, to be precise), and a link to the next node in the list (or a null pointer if there is no such next node).本质上,链表由节点组成,每个节点包含一个数据(一个对象,准确地说,它本身可以包含多个成员变量)和一个到链表中下一个节点的链接(或者一个空指针,如果有的话)没有这样的下一个节点)。 You can also have a doubly-linked list, where each node also has a pointer to the previous node in the list, to speed up certain kinds of access patterns.您还可以使用双向链表,其中每个节点还有一个指向列表中前一个节点的指针,以加快某些类型的访问模式。

To add multiple "pieces of data" to a single node sounds like adding several links off of one node, which turns your linked list into an N-ary tree .将多个“数据片段”添加到单个节点听起来像是从一个节点添加多个链接,这会将您的链表变成 N 元

To add multiple pieces of data onto the end of the list, in the manner most commonly associated with a linked list, just do:要将多条数据添加到列表的末尾,以最常与链表关联的方式,只需执行以下操作:

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);

Alternately, if you want each node of the linked list to have several pieces of data或者,如果你想让链表的每个节点都有几条数据

These data should be packaged up into an object (by defining a class that has them all as member variables).这些数据应被打包成一个对象(通过定义一个class具有它们都作为成员变量)。 For example:例如:

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);
  }
}

Now, linky will have 2 nodes, each of which will contain 4 int s, myInt1 , myInt2 , myInt3 , and myInt4 .现在, linky将有 2 个节点,每个节点将包含 4 个int s、 myInt1myInt2myInt3myInt4

Note注意

None of the above is specific to linked lists.以上都不是特定于链表的。 This pattern should be used whenever you want to store a bunch of data together as a unit.每当您想将一堆数据作为一个单元存储在一起时,就应该使用这种模式。 You create a class that has member variables for every piece of data you want to be stored together, then create any Java Collections type (ArrayList, LinkedList, TreeList, ...) of that type.您创建一个类,该类具有要存储在一起的每条数据的成员变量,然后创建该类型的任何 Java 集合类型(ArrayList、LinkedList、TreeList 等)。

Be sure that you want to use a linked list (as there's no penalty in terms of programming difficulty in choosing an ArrayList or TreeList).确保您要使用链表(因为在选择 ArrayList 或 TreeList 时不会增加编程难度)。 This will depend on your data access pattern.这将取决于您的数据访问模式。 Linked lists provide O(1) addition and deletion, but O(n) lookup, whereas ArrayLists provide O(1) lookup, but O(n) arbitrary add and delete.链表提供O(1)添加和删除,但O(n)查找,而ArrayLists提供O(1)查找,但O(n)任意添加和删除。 TreeLists provide O(log n) insertion, deletion, and lookup. TreeLists 提供 O(log n) 的插入、删除和查找。 The tradeoffs between these depend on the amount of data you have and how you're going to be modifying and accessing the data structure.这些之间的权衡取决于您拥有的数据量以及您将如何修改和访问数据结构。

Of course, none of this matters if you'll only have, say, <100 elements in your list ;-)当然,如果您的列表中只有 <100 个元素,那么这些都不重要;-)

Hope this helps!希望这有帮助!

Use a structure.使用结构。

For example:例如:

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);

Note;注意; I assume this is in C#;我假设这是在 C# 中; correct me if I'm wrong and I will fix the code ;)如果我错了,请纠正我,我会修复代码;)

If you have not gone over OOP yet in your book - then I would recommend giving it a try;如果您还没有在书中讨论 OOP - 那么我建议您尝试一下; it will help you solve problems like this.它将帮助您解决此类问题。

Why not something like that:为什么不是这样的:

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

Like Nelson said you need another Object, in Java though you need to use a Class.就像纳尔逊说的你需要另一个对象,在 Java 中虽然你需要使用一个类。 If you need the 'Node' Class to be used outside the Class you're working in, then you need to make it a Public class, and move it to it's own file.如果您需要在您正在使用的类之外​​使用“节点”类,那么您需要将其设为公共类,并将其移动到它自己的文件中。

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);

Apologies to Nelson for stealing his code ;)向尼尔森道歉,因为他窃取了他的代码;)

Here's a complete code sample that shows the use of adding a structure to a linked list:这是一个完整的代码示例,展示了向链表添加结构的用法:

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);
    }
}

I don't really understand what you are trying to achieve, so I suggest a solution to another reading of the problem (in java).我不太明白你想要达到的目标,所以我建议另一种阅读问​​题的解决方案(在 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
}

I personally happen to like Nelson's solution best if the amount of numbers to add is constant (num1 .. num4), and if it is not constant I would prefer Gregor's solution (who uses a List in stead of a Node).如果要添加的数字数量恒定(num1 .. num4),我个人碰巧最喜欢 Nelson 的解决方案,如果它不是恒定的,我更喜欢 Gregor 的解决方案(他使用 List 而不是 Node)。 If you go for the Node method in java I suggest:如果您使用 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

Lot's of nitpicking, but I think a static class in stead of a none-static private class is preferred if possible (and it normally should be possible).很多吹毛求疵,但我认为如果可能的话,最好使用静态类而不是非静态私有类(通常应该是可能的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM