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.
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.
PS: i'd like to achieve this in Java
CodePudding user response:
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. You can use LinkedList of 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*/);
CodePudding user response:
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). Since data can be anything, you can define a List
of the type of your choice. Example:
List<int[]> myList = new ArrayList<int[]>();
and then you can define each element as an array of n elements. You can use other data types as well.