[英]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.