Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Defining a Node Structure for a Singly Linked List

Welcome to a new module! In the previous one, you mastered array-based linear data structures like stacks and queues. You learned that arrays store elements in a single, contiguous block of memory. While powerful, this approach has limitations, especially when you need to insert elements efficiently or don't know the size of your data in advance.

Today, we begin our journey into linked lists, a dynamic data structure that offers a more flexible way to organize data. Instead of a contiguous block, a linked list is a chain of individual elements scattered throughout memory, connected by pointers. Our first step is to understand and define the fundamental building block of this chain. Your goal for this lesson is to learn how to define a node struct for a singly linked list.

1. The Anatomy of a Node

A linked list is composed of a series of nodes. Each node is a small container with two distinct parts.

This diagram illustrates the fundamental structure of a linked list node. It contains the data itself and a pointer that forms the link to the next node in the sequence.

As shown in the diagram, a node consists of:

  1. Data: The actual value you want to store. This could be a simple integer, a floating-point number, or even a complex struct.
  2. Link/Next: A pointer that holds the memory address of the next node in the chain. For the very last node in the list, this pointer is set to NULL to signify the end.

This structure allows nodes to be located anywhere in memory, providing great flexibility for adding and removing items.

2. From Concept to C Code: The Self-Referential Struct

To implement this node concept in C, we need a way to group the data and the next pointer together. As you learned in Module 5, a struct is the perfect tool for this. However, there's a unique and powerful twist.

The next pointer inside our struct must be able to point to another struct of the exact same type. This creates what is known as a self-referential structure. It's a structure that contains a member that points back to its own type.

Let's watch a short video that explains this concept and demonstrates how to define a node in C.

Creating the Node of a Single Linked List

This video, "Creating the Node of a Single Linked List" by Neso Academy, will introduce you to self-referential structures and show you how to define the struct node.

Please watch the first two parts of the video: Start with the introduction to self-referential structures. Then, continue to the section explaining how this concept is used to define a node. Focus on how the visual concept of a node maps to the C struct definition.

3. The struct node Definition

The video provided a clear breakdown of how to write the C code. Let's examine the definition closely. This is the core piece of code for this entire module.

struct node {
    int data;
    struct node *next;
};

Let's break this down line-by-line:

  • struct node { ... };: This declares a new user-defined type named struct node.
  • int data;: This is the data member. For simplicity, we are using an int, but this could be any type you need (float, char[], or another struct).
  • struct node *next;: This is the self-referential part. We declare a pointer named next. Its type, struct node *, signifies that it is designed to hold the memory address of another struct node instance.

To make the type easier to use, it's common practice to use typedef to create an alias, like this:

typedef struct node {
    int data;
    struct node *next;
} Node;

// Now you can declare a pointer like this:
Node *my_node_pointer;

For our lessons, we'll stick to the struct node syntax to be explicit, but it's good to recognize this common pattern.

To further solidify your understanding, let's look at another excellent explanation.

CS31: Intro to C Structs and Pointers

This document provides a great summary of structs and pointers, including a dedicated section on defining linked list nodes.

Please scroll down to the section titled "Linked Lists in C". Read this short section to see the struct node definition again and a brief example of how it's used to build a list.

4. Visualizing a Node in Memory

You now know how to define a struct for a node. But what happens when you actually create one? This process combines your knowledge of structs and dynamic memory allocation from Module 5.

To create a node, you use malloc to request a chunk of memory from the heap that is large enough to hold one struct node.

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

The following image shows both the C code to create two nodes and a diagram of what happens in memory as you link them together.

This image shows the C code for defining a `struct node` and then creating two nodes. The diagram on the right illustrates how the `next` pointer of the first node is updated to hold the address of the second node, forming a two-element linked list.

The key operation shown is nodeFirst.next = &nodeSecond;. This line takes the memory address of nodeSecond and stores it in the next pointer field of nodeFirst, creating the "link" in the chain.

Conclusion

In this lesson, you've taken the first and most critical step in understanding linked lists. You've moved from the concept of a "node" to its concrete implementation in C.

Key Takeaways:

  • A linked list is a sequence of nodes, where each node can be located anywhere in memory.
  • Each node contains two essential parts: the data and a pointer (next) to the subsequent node.
  • In C, a node is implemented using a self-referential struct. This is a struct that contains a pointer member whose type is the struct itself (e.g., struct node *next;).
  • Creating a node at runtime is done using dynamic memory allocation with malloc(sizeof(struct node)).

Preview of the Next Lesson:
We have successfully defined our building block. Now it's time to start building. In the next lesson, you will use this struct node definition to write your first linked list function: a function to insert a new node at the head of a list. This will be our first step towards creating and manipulating a complete linked list.

Can't find a good explanation? Sign up and we'll make it for you

Sign up