简体   繁体   English

我如何制作像链表这样的数据结构,但每个节点都包含 N 个元素?

[英]How could i make a data structure like linked list, but each node holds N elements?

So I want to make a linked list type of structure in the sense that each "group" of N elements also holds either a NULL pointer or a pointer to the next group.所以我想创建一个链表类型的结构,每个 N 元素的“组”也包含一个 NULL 指针或指向下一组的指针。

So for example, I read an integer of 4, i want then to create a structure which hold 4 integers plus a NULL pointer, and then later if I wish to create another group of 4 integers i can change the NULL pointer of the first group to point to the second group.例如,我读取了一个 4 的整数,然后我想创建一个包含 4 个整数和一个 NULL 指针的结构,然后如果我想创建另一组 4 个整数,我可以更改第一组的 NULL 指针指向第二组。

PS: i'd like to achieve this in Java PS:我想用 Java 实现这个

If I understood your question correctly, you want to add a group of N elements to a data structure, and then this group can reference another group of M elements, and so on.如果我正确理解您的问题,您想将一组 N 个元素添加到一个数据结构中,然后该组可以引用另一组 M 个元素,依此类推。 You can use LinkedList of LinkedList.您可以使用 LinkedList 的 LinkedList。

    LinkedList<LinkedList<Integer>> outerList = new LinkedList<>();
    
    int input = 4;
    LinkedList<Integer> innerList = new LinkedList<>();
    for (int i = 0; i < input; i++) innerList.add(i);
    outerList.add(innerList);
    
    input = 3;
    innerList = new LinkedList<>();
    for (int i = 0; i < input; i++) innerList.add(i);
    outerList.add(innerList);

    
    LinkedList<Integer> myList = outerList.get(/*the index of the group you want*/);
    myList.get(/*the element you want from that group*/);

Linked list is a data-structure that consists of nodes.链表是一种由节点组成的数据结构。 Each node has a data and a next.每个节点都有一个数据和一个下一个。 The next is another node (or null if you are at the end of the list).下一个是另一个节点(如果您在列表的末尾,则为null )。 Since data can be anything, you can define a List of the type of your choice.由于数据可以是任何东西,您可以定义您选择的类型的List Example:例子:

List<int[]> myList = new ArrayList<int[]>();

and then you can define each element as an array of n elements.然后你可以将每个元素定义为一个包含 n 个元素的数组。 You can use other data types as well.您也可以使用其他数据类型。

暂无
暂无

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

相关问题 Java Linked List 如何创建一个包含字符串和int的节点? - Java Linked List How to create a node that holds a string and an int? 链表的数据结构 - Data structure for linked list 在每个索引处保存两个对象的数据结构(Java) - Data Structure that holds two objects at each index (Java) 如何使用java.util.ListIterator输出保存对象的Linked List? - How do I output Linked List which holds objects using java.util.ListIterator? 如何访问链表中每个节点的不同元素 - How to access different element of each node in the linked-list 哪种数据结构更适合使用顺序搜索来搜索元素 - 数组列表或链接列表? - Which data structure is better for searching elements using sequential search - Array List or Linked List? Java 8:如何更新链表中的数据,这里我需要拉取前一个节点的数据来更新当前节点的数据 - Java 8 : How to update data in a linked list, where I need to pull the data of previous node to update data of current node 链表是抽象数据类型还是数据结构? - Is a linked list an abstract data type or a data structure? 如何遍历链表并打印与输入匹配的每条数据? - How can I loop through a linked list and print each piece of data that matches the input? 如何创建树状结构,其中有不同类型的节点,并且每个节点都可以引用任何节点? - How do I create a tree like structure in which there are different types of nodes and each node can have references to any of the nodes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM