Download Introduction to Linked List in C | Data Structures Study Notes with Examples for CS and more Study notes Computer Science in PDF only on Docsity!
DATA STRUCTURES
Introduction to Linked List
(Language used for code: C)
What is a Linked List โข Why Needed vs Arrays โข Node Structure โข Array vs Linked List Comparison
A Complete, Fact-Checked Study Guide for Computer Science Students
Contents
1. What is a Linked List?
2. Why Linked Lists Are Needed (Limitations of Arrays)
3. Node Structure (Data + Pointer)
4. How Nodes Connect to Form a List
5. Basic C Code โ Defining and Creating Nodes
6. Array vs Linked List โ Detailed Comparison Table
7. Memory Representation: Array vs Linked List (Diagram)
8. Solved Examples
9. Quick Revision Summary
traversed from the HEAD, taking O(n) time) and by using extra memory for storing pointers in every
node. Choosing between an array and a linked list depends entirely on which operations (access vs
insertion/deletion) are more frequent in the given problem.
3. Node Structure (Data + Pointer)
The fundamental building block of any linked list is the node. Every node is divided into two distinct
parts:
- Data part:^ Stores the actual value (e.g., an integer, character, or any other data type).
- Pointer part (also called 'next'):^ Stores the memory address of the next node in the list. If there
is no next node (i.e., this is the last node), the pointer stores a special value called NULL.
Diagram: Structure of a single node
data next
Data part Pointer part
points to next node
Figure 1: A single node contains a data field and a pointer field that stores the address of the next node.
3.1 Node Structure in C
struct Node { int data; // data part struct Node* next; // pointer part โ address of the next node };
In this definition, data holds the value being stored, and next is a pointer variable of type struct Node ,
meaning it can hold the address of another node of the same structure. This self-referential structure is
what allows nodes to be linked together into a chain.
4. How Nodes Connect to Form a List
A linked list is formed when multiple nodes are connected in sequence: the pointer of the first node
stores the address of the second node, the pointer of the second node stores the address of the third
node, and so on. The very last node's pointer is set to NULL, clearly marking the end of the list. A
separate pointer variable, conventionally called HEAD, always stores the address of the first node so
that the list can be accessed from the beginning.
Diagram: A chain of three linked nodes
10 25 47 NULL
HEAD
Figure 2: HEAD points to the first node (10). Each node points to the next, and the last node (47) points to NULL, marking the end of the list.
6. Array vs Linked List โ Detailed Comparison Table
Feature Array Linked List
Memory allocation Contiguous (one continuous block) Non-contiguous (nodes scattered in memory)
Memory overhead None extra โ only stores data Extra memory per node used to store the pointer
Size Fixed at creation (static array); resizing requires a new array and copying
Dynamic โ grows or shrinks at runtime, one node at a time
Access time (by position) O(1) โ direct index-based access O(n) โ must traverse from HEAD
Search time O(n) (linear search on unsorted data) O(n) (must traverse node by node)
Insertion at beginning O(n) โ all elements shift right O(1) โ only update HEAD pointer
Insertion at end O(1) if space available, else O(n) O(n) unless a tail pointer is maintained (then O(1))
Insertion at middle O(n) โ elements after the point shift O(n) to find the position, then O(1) to insert
Deletion (beginning) O(n) โ all elements shift left O(1) โ only update HEAD pointer
Deletion (middle/end) O(n) โ elements shift to fill gap O(n) to find the node, then O(1) to delete
Cache performance Better โ contiguous memory is cache-friendly
Weaker โ scattered memory causes more cache misses
Ease of implementation Simple Relatively more complex (pointer handling)
Note on accuracy: The O(1) insertion/deletion at the beginning for a linked list assumes the HEAD
pointer is directly available โ no traversal is needed to reach the first node. For positions other than the
head (or tail, if a tail pointer exists), the list must first be traversed to locate the correct node, which
takes O(n) time; only the pointer update itself is O(1).
7. Memory Representation: Array vs Linked List
The core structural difference between the two data structures becomes clearest when their memory
layout is visualised side by side.
Array (contiguous memory)
1000
1004
1008
1012
Linked List (scattered memory)
addr 2050
addr 3110
Figure 3: An array stores elements at fixed, evenly-spaced, contiguous addresses. A linked list stores nodes at^ addr 2600 scattered addresses, connected only through stored pointers.
Because array elements sit at evenly spaced addresses, the address of any element can be calculated
directly using Base Address + (index ร element size). A linked list has no such formula โ the only way
to reach the third node is to start at HEAD and follow exactly two pointers.
8. Solved Examples
Solved Example 1: Trace pointer connections
Problem: A linked list has 3 nodes at memory addresses 200, 500, and 350 (in the order they were
created: Node A at 200, Node B at 500, Node C at 350). If HEAD = 200, Node A's next = 500, Node B's
next = 350, and Node C's next = NULL, write the order in which the nodes are visited during traversal.
Step-by-step solution:
- Start at HEAD = 200^ โ^ this is Node A.
- Node A's next = 500^ โ^ move to Node B.
- Node B's next = 350^ โ^ move to Node C.
- Node C's next = NULL^ โ^ traversal ends.
Final Answer: Traversal order is Node A โ Node B โ Node C, even though their physical memory
addresses (200, 500, 350) are not in increasing order โ this demonstrates that logical order in a linked
list depends on pointers, not on physical memory position.
Solved Example 2: Count nodes in a linked list (C code)
Problem: Write C code to count the total number of nodes in a linked list given its HEAD pointer.
int countNodes(struct Node* head) { int count = 0; struct Node* temp = head; while (temp != NULL) { count++; temp = temp->next; } return count; }