DEV Community

Cover image for Linked List in C
João Vitor Amorim
João Vitor Amorim

Posted on

Linked List in C

Linked List's are a simple way to store relational data; some of it's possibilities are: you can access any particular node, modify it, add nodes in the head/tail, reverse the list & much more.

Lest's start by creating our struct that will hold a Int Value and a pointer(which will point to the next node)

Alt Text

Now we have to creat a list with the Node struct:

-> We have to allocate a space in our memory for the first node
-> Add data in our node
Alt Text
Since it's the first node it can point to NULL.

Congratulations, you manage to implement your linked list.

The next step is to creat a function that add's nodes:

-> Pass the reference of the head of the list & the value that will be stored
-> Find the last node
-> Allocate the memory for the new node
-> Make the last node reference point to the new node
-> Make the new node recieve the value
-> Make the new node point to NULL

Alt Text

To Finish let's create our final function to print the list.

-> Pass the reference of the head of the list
-> Print the value in the current node
-> Check if the next node exists
-> Call the function again for the next node

Alt Text

Now the implimentation is complete, in the next posts I will cover how to reverse a Linked List (Both iterative and recursive)

Thanks for your time, see you :)

Top comments (0)