DEV Community

Gazel-create
Gazel-create

Posted on

Journey towards Mastering (Computers)

Being persistent is hard when you have responsibilities, when you need to work to earn money, when you don't have time to do what you want. This is what I was telling myself every day, when I missed a daily coding challenge, when I couldn't finish a project on time, when I lay in bed tired.

Motivation is not necessary. Just do it. Love your fate. This post is my special way of showing myself how much I want this. I have been learning computer science and doing things that I keep forgetting due to a lack of reinforcement. Hence, I have made a 3-month plan to learn and relearn all the basics to make myself a better programmer. This is my progress for Day 1.

Also, I will not post Day 1, 2, 3, etc. for 90 days straight. I will only post when I have time, or when I have learned something significant that makes me smile or let out a small giggle that makes me look like a psycho hehe.

Day 1 : Single Linked List

I started the task having an idea of what a linked list was, but I had no idea about the different varieties of linked lists:

  • Singly Linked List

  • Doubly Linked List

  • Circular Linked List

  • Double Circular Linked List

I started Day 1 by writing a few lines of code to make a singly linked list. I will just paste the program code right now, and then I will write about what was interesting to me.

#include <stdio.h>
#include <stdlib.h>


struct node {
    int x;
    struct node *ptr;
};



int main (){
    int value[] = {10, 20, 30};

    struct node *head = NULL;

    for (int x = 0; x < 3; x++){

        struct node *new_node = malloc(sizeof(struct node));

        new_node->x = value[x];
        new_node->ptr = head;

        head = new_node;    

        }

    struct node *current = head;
    while (current != NULL){
        struct node *next_node = current->ptr;

        free(current);
        current = next_node;
    }

    head = NULL;
    return 0;

}
Enter fullscreen mode Exit fullscreen mode

First thing was, I have used C++ before. Then, while trying to understand objects, I read a line from Gemini that said objects are just a cooler version of structs. Well, custom data structures are kinda like objects, I guess, it's just that you cannot put functions inside of structs. Also, the idea of custom data types is fun, and I have come to appreciate memory management with the help of C.

The code is simple. It creates a singly linked list, where we create nodes using the struct data type. It uses pointers to new heap addresses, which are the size of the struct that we created. Later, we will destroy the memory one after another using free().


struct node {
    int x;
    struct node *ptr;
};
Enter fullscreen mode Exit fullscreen mode

Here, in the code above, you can see we referenced a pointer of the data type node that we haven't even finished creating. Do you see it? It is called a self-referencing struct, and the magic happens due to pointers. Yup, pointers are the MVP; the magical, fantastic idea of pointers. The compiler just knows and says, "Here I am gonna allocate 8 bytes for a 64-bit system, 'cause it is a pointer'.

The core idea of a linked list is to have a starting point to reference the first node, and then we can start going to the next struct one by one.

struct node *head = NULL;
Enter fullscreen mode Exit fullscreen mode

Always remember to delete/free the memory after use. Here, I have assigned a new struct to hold the pointer value to the next struct.
'Cause the heap still has the nodes, I just copied the location of head and backtracked it, Last In First Out (LIFO) style. Fun fact:

current->ptr
//same as
(*current).ptr
Enter fullscreen mode Exit fullscreen mode

We are just accessing the member ptr of the struct after dereferencing to follow the pointer to its actual memory address.

In the end, it is a good practice to reset the pointer so that it does not cause any errors like a dangling pointer. Even though we deleted the memory address that head was pointing to, head will still point to that deleted address unless we reset it.

Finally, I wanna say it was fun writing this blog/article, I don't know. I will keep posting my progress as I continue to peer deep into the colourful and blinding grandness of programming. Thank you for reading this, have a wonderful day.

Top comments (2)

Collapse
 
udpfnhvikotm profile image
simimeow:3

good luck with your journey!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.