Course contentsShow
← Computer Science
Lesson 325 of 2,8728. Linked Data StructuresFree lesson

Node Structure

Defining a node with data field and next pointer; the building block of singly linked lists.

Node Structure

What you'll learn: You'll learn how to define a node—the basic building block of a linked list—containing data and a pointer to the next node.

What Is a Node?

A node is a self-contained package that holds two essential pieces:

  1. Data field – the actual value you want to store (an integer, string, etc.)
  2. Next pointer – the memory address of the next node in the chain

Think of a node like a train car: it carries cargo (data) and has a coupler (pointer) that connects it to the next car. Unlike array elements that sit side-by-side in memory, nodes can be scattered anywhere—the pointer is what links them together.

Defining a Node Structure

Here's how you define a node for a singly linked list:

struct Node {
    int data;           // The value stored in this node
    struct Node* next;  // Pointer to the next node in the list
};

Notice the self-referential nature: a Node contains a pointer to another Node. This is legal and powerful—it's how the chain forms.

Creating a Single Node

struct Node* firstNode = (struct Node*)malloc(sizeof(struct Node));
firstNode->data = 42;
firstNode->next = NULL;  // No node follows (this is the end)

Here we've dynamically allocated memory for one node, stored 42 in its data field, and set next to NULL (meaning "there's nothing after me").

Key Takeaway: A node is a structure with a data field and a next pointer; it's the fundamental building block that enables linked lists to chain elements together dynamically in memory.