So I've been trying to understand the concepts of linked lists (Been looking at some example code, I found this one on the internet. Now if I could kindly have someone confirm if I have grasped some of the concepts correctly. I will draw diagrams of what I think each linke of code does.
#include <stdio.h>
#include <stdlib.h>
struct ListItem {
int data;
struct ListItem *next;
};
int main (int argc, char *argv[]){
struct ListItem a;
a.data = 0;
a.next = NULL;
struct ListItem b;
b.data = 1;
b.next = NULL;
struct ListItem c;
c.data = 2;
c.next = NULL;
a.next = &b;
b.next = &c;
int counter = 0;
struct ListItem *i;
for (i = &a; i != NULL; i = i->next){
printf("Item %d value is %d\n\r", counter, i->data);
counter++;
}
return 0;
}
Snippet of code 1:
struct ListItem {
int data;
struct ListItem *next;
};
This creates a structure called ListItems. There are two components to the structure, a component to store the data and a pointer to another structure of type struct ListItem. I visualise the linked list like this:

Is this a correct way of visualising it?
Code snippet 2:
struct ListItem a;
a.data = 0;
a.next = NULL;
struct ListItem b;
b.data = 1;
b.next = NULL;
struct ListItem c;
c.data = 2;
c.next = NULL;
Yup, I know it can be cut shorter but I just did it this way to see if I can understand the concept. Now this snippet creates a variable "a", "b" and "c" of type struct ListItem. Then it sets the first member (data) of each structure to 0, 1, 2 respectively and the second member (next) to point to NULL. So my visualisation now is like this:

Now MORE QUESTIONS:
Question 1: When we are initially the pointer to NULL, it points to nothing correct? Why do we do this? Wasn't it originally pointed to nothing?
Snippet 3:
a.next = &b;
b.next = &c;
This let's next in each variable a, b (which is a structure) point to the address memory location of b and c respectively.
My visualisation: 
Question: How can it do this? Isn't the structure itself stored over multiple memory address (4 for int, etc)
Snippet 4:
int counter = 0;
struct ListItem *i;
for (i = &a; i != NULL; i = i->next){
printf("Item %d value is %d\n\r", counter, i->data);
counter++;
}
Here is the snippet I've been a bit confused with. Now, we set aside an integer called counter and initailise this to zero. Also, we create a variable called i that points to the type struct ListItem. Now could someone explain to me the for loop? I am a bit confused on what it's doing. In particular, i=i->next, I'm not familiar with this. I know it's equivalent to i=(*i).next but not sure what it really does. Could someone create a quick diagram?
ALSO: If anyone has any good resources/links (no pun intended) to some useful websites to help me understand linked lists a bit better feel free to post them.
Q1: an uninitialized pointer does not point to "nothing". If you don't set its value, it can (and will) point to just about anything, including (but not limited to) a nul value, out-of-bounds memory, or the last password you entered on a website.
It's really no different than asking what the value of your counter is when you do
int counter;
...
printf ("counter is %d\n", counter);
Setting the next pointer to NULL, therefore, serves two distinct purposes. First, you are sure it is a "known" value, not any random one. Second, by convention a NULL value means that it does not point to any valid location.
(It is a 'convention' because NULL effectively stores the numerical value '0'. This, as a memory address -- since you are talking about pointers -- is perfectly valid. There are just not many daily uses for it.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With