C Nested Linked list using dot for members


Is it possible to have a nested linked list in C using dot operator like structs to access members?

Should be able to create new instances using loops
I don't understand exactly what you are asking, but the dot operator in C is only used to access the members of an object. You can not make it do anything other than that. Not even C++ allow overloading the dot operator.
Last edited on
you can make a linked list of linked lists. in c++ its easier and cleaner with templates. in C you might have either 2 list objects (one of data and one of lists of data) or a generic one that stores void*s. If its void* generic you access the data by casting all the pointers back, which would be rather messy, and your find/delete/etc methods need some way to know what to cast to so they can take a look. And the list of lists needs some sort of ID field on each list as well, of course.

and finally you could create a 2-d like structure in a single object that was internally a list of lists.

All in all this seems like a weird and less than useful design. To me it feels like an array (or pointer) of lists is more useful, if you need many, where each one has a fixed location you can jump to (remember the list ID? Now its the array/pointer offset index). But maybe you have an interesting problem where it makes sense to list the outside instead? I could see it also where you would not have search/delete/etc type functions, like a list used for a stack, queue, etc and you only work of the endpoints.
Last edited on
@ruzip - what are you trying to achieve? What is the requirement you're trying to meet?

I already specified in my post:

A nested linked list in C using dot operator for assigning values to members.

@jonnin

Thanks for the info, but would like to get some ideas or examples.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Node {
  int value;
  Node* next {};
};

struct NestNode {
  Node* valp {};
  NestNode* next {};
};

NestNode head = ...

head->next->valp->value = 42; // A
(*head).(*next).(*valp).value = 42; // B 

Both A and B do assign a value to (existing) first element of second list of the nest pointed to by the head.
The latter does use the dot operator.
The struct definition syntax does differ in C. (Can't recall how.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
My C is rusty, but some pseudocode..

struct node  //c needs a typedef, c++ makes this a type directly. 
{
   void* data;
   int ID;
   node* next;
};
void add(node &head, void* d)
{
  static int id = 1;
  ... add to the list here
}

node *outerlist = malloc(..)
node *innerlist1 = malloc(...

for(whatever)
{
   add(innerlist1, something);
}
add(outerlist,innerlist1);
... repeat for innerlist2..3..4..n

and later.. heh.. is where it gets unfriendly you will get something like this..
(data*)(find(((node*)(find(outerlist, idnum))),id))
to get say an integer (data* means int* in that case) from one of the inner lists...


do you see how much easier it is to instead say (if you can)
node* outer[numlists];
for(i = ..numlists)
for(items in each list..)
add(outer[i], item)

nesting the lists is going to be an exercise in frustration and if you can possibly avoid that, do so... the answer is that yes its possible, but maybe you should try to find another way before going there.
Last edited on
Are you trying to do something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Inner
{
   int inner_a;
   int inner_b;
};

struct Outer
{
   Inner outer_a;
   Inner outer_b;
};

int main()
{
   Outer a;

   a.outer_a.inner_b = 12;
}
@Keskiverto
@GeorgeP

Yes something like that.

I'd prefer the dot type yes if possible.

@jonnin

I agree, linked list and loops are a pair.



I realized using struct arrays would make this happen but keeping track of the current or managing it would be more complicated.
Last edited on
Topic archived. No new replies allowed.