+ 1
Why is there structure in the same structure ? What does that mean ? And can you give me an Example? Thanks..
Struct Element { Struct Element *pointer; };
2 Antworten
+ 2
You use this for linked lists, so you can make an item point to the next item in the list.
struct list
{
int i; /* and whatever other data you need */
struct list* next;
};
You can then connect elements of this type in a chain:
struct list a = {1, NULL};
struct list b = {2, &a};
struct list head = {3, &b};
Now your data is chained as: 3 - 2 - 1:
head.next is a pointer to b, so head.next->i gives 2
head.next->next is a pointer to a, so head.next->next->i gives 1
You can also loop through your list:
struct list* p = &head;
while (p != NULL)
{
printf("value: %d\n", p->i);
p = p->next;
}
(Please keep in mind: all code was simply typed in without compile or check if it actually works)
Do have a look at the wikipedia page about linked lists:
https://en.wikipedia.org/wiki/Linked_list
+ 1
Here is a nice Sololearn article about linked lists: https://www.sololearn.com/learn/634/?ref=app