Linked List Implementation using C

“In their most basic sense, linked list and blockchain are very similar. A node (or block in case of the latter) contains the address (or hash) of the next node.”

Image source: me + MS Paint

My first encounter with linked list was in the 2nd semester of my undergrad study. Since we only use C/C++ in our university, I felt like I had to make some adjustments on my usual programming environment; I usually use Python in Pycharm and Swift on XCode. Unbeknown to me, XCode also support C/C++, with built-in realtime error checker!. So yeah, in the end, I use XCode instead of VSCode or CLion.

As stated in the title, I used C, not C++, so there are no classes and objects.

The first step is to create a struct containing the placeholder of the data and pointer to the next node (AKA the struct itself).

struct Node { 
        int data;
struct Node *next;
} *previous, *start, *current;

What are those three on the last line?

Those are struct variables; instance of the struct itself. Remember, when we defined Node using keyword struct, we are creating a blueprint of a collection of variables. Those three variables are the instance of the struct.
Typing them that way is the same thing as typing these:

struct Node *previous, *start, *current;

inside the main function.

If you’re familiar with object-oriented programming, we are essentially creating a class and objects. Only this time, a struct and struct variables.

Now, of course, we want to start filling the linked list with data. But how?

start = NULL; 

while(1) {
        //allocate memory for current node
        current = malloc(sizeof(struct Node));

        //take input from user
        scanf("%d", &current->data);
        
        //just enter 123123 to end this cycle
        if(current->data == 123123) {
            break;
        }

        //make sure the current node is the last node
        current->next = NULL;
        
        //for the first time, make the current node
        //as the starting node
        if (start == NULL) start = current;
        
        //for the subsquent iterations, make the tail
        //of the previous node points to the current node
        else previous->next = current;
        
        //take the content of current node
        //into the previous node
        previous = current;
}

That’s it!

After entering 123123 to end the cycle, if we want to traverse through all the nodes, we just need to do

struct Node *n = start;
    
while (n!= NULL) {
        printf("%d ", n->data);
        n = n->next;
}

Why do we use n!=NULL?

The last node in our linked list terminates the cycle after we enter

current->next = NULL;

hence the *next pointer of our last node doesn’t point anywhere. So when the while loop reaches n==NULL, it terminates itself.

I will post more code on insertion and deletion in the next blog post.

Leave a comment

Website Powered by WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started