This article is part 1 of 1 in the series Singly Linked Lists in C
  • Part 1 – Singly Linked Lists in C Programming Language

Introduction

This article covers some basics of linked lists, what they are, how to build them using C programming, and how to manipulate them. You will need a bit of knowledge of how pointers are used in C programming. For now, just think that pointers are variables which point to another variable. There is a concept called pointer arithmetic, which is great if you know some of it. Linked lists are a form of data structures which are different than arrays.

What are Linked Lists?

Linked Lists are data structures. They are variables which contain other variables. There are multiple types of Linked Lists in general e.g. singly Linked Lists, Doubly Linked Lists, and Circular Linked Lists. There are also different forms of implementations as well e.g. one such implementation is in Linux Kernel where the data is separate from the list itself, but still linked. In this article, as the title says, I will be only covering the generic Singly Linked Lists.

Linked Lists are like chains, which contain a reference to another Linked List of the same type. Singly Linked Lists contain the reference to the next Linked List in sequence. The data is stored in a non-contiguous manner as compared to Arrays. And the chains can be shrunk or expanded on demand at the runtime. Whereas in Arrays you define the size at the compile time, and the data is also contiguous.

Linked Lists are also a great way to learn about advanced pointers.

How does it look like?

In the above snippet, the node data structure has a pointer named next which is pointing to the location where the linked node is stored. The other variable is data and you can have any number of variables.

Build a simple Linked List

Here is an example of how you can build a very simple Linked List.

 

Now that it is built, in the next part, I will show how to access the elements in the list.