Open In App

Linked List in C

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A linked list is a linear data structure where each element (called a node) is connected to the next one using pointers. Unlike array, elements of linked list are stored in random memory locations.

In this article, we will learn about the linked list, its types, representation of the linked list in C, and discuss what link list offers as compared to the similar data structures.

What is a Linked List?

A linked list is a sequence of nodes in which each node is made up of two parts:

  • Data: The value stored in the node.
  • Pointer: A reference to the next node in the sequence. (There can be multiple pointers for different kind of linked list.)

Unlike arrays, linked lists do not store nodes in contiguous memory locations. Instead, each node contains pointer to the next node, forming a chain-like structure and to access any element (node), we need to first sequentially traverse all the nodes before it.

It is a recursive data structure in which any smaller part of it is also a linked list in itself.

Representation of Linked List in C

In C, linked lists are represented as the pointer to the first node in the list. For that reason, the first node is generally called head of the linked list. Each node of the linked list is represented by a structure that contains a data field and a pointer of the same type as itself. Such structure is called self-referential structures.

Types of Linked List in C

Linked list can be classified on the basis of the type of structure they form as a whole and the direction of access. Based on this classification, there are five types of linked lists:

  1. Singly Linked List
  2. Doubly Linked List
  3. Circular Linked List

Let's discuss about each of them.

Singly Linked List in C

A linked list or singly linked list is a linear data structure that is made up of a group of nodes in which each node has two parts: the data, and the pointer to the next node. The last node's (also known as tail) pointers point to NULL to indicate the end of the linked list.

singly-linked-list-in-c
Singly Linked List

Representation of Singly Linked List

A linked list is represented as a pointer to the first node where each node contains:

  • Data: Here the actual information is stored.
  • Next: Pointer that links to the next node.
C
// Structure to represent the 
// singly linked list
struct Node {
  
  // Data field - can be of 
  // any type and count
  int data;
  
  // Pointer to the next node
  struct Node* next;
}

Basic Operations on Singly Linked List

Operation

Operation TypeDescriptionTime ComplexitySpace Complexity

Insertion

At BeginningInsert a new node at the start of a linked list.O (1)O (1)
At the EndInsert a new node at the end of the linked list.O (N)O (1)
At Specific PositionInsert a new node at a specific position in a linked list.O (N)O (1)

Deletion

From BeginningDelete a node from the start of a linked listO (1)O (1)
From the EndDelete a node at the end of a linked list.O (N)O (1)
A Specific NodeDelete a node from a specific position of a linked list. O (N)O (1)

Traversal

Traverse the linked list from start to end.O (N)O (1)

Doubly Linked List

A doubly linked list is a bit more complex than singly linked list. In it, each node contains three parts: the data, a pointer to the next node, and one extra pointer which points to the previous node. This allows for traversal in both directions, making it more versatile than a singly linked list.

doubly-linked-list-in-c
Doubly Linked List

Representation of Doubly Linked List

A doubly linked list is represented as a pointer to the first node (head), where each node contains:

  • Data: The actual information stored in the node.
  • Next: A pointer that links to the next node in the sequence.
  • Previous: A pointer that links to the previous node in the sequence.
C
// Structure to represent the doubly linked list
struct Node {
  
  // Data field - can be of any type and count
  int data;
  
  // Pointer to the next node
  struct Node* next;
  
  // Pointer to the previous node
  struct Node* prev;
}

Basic Operations on Doubly Linked List

Operation

Operation TypeDescriptionTime ComplexitySpace Complexity

Insertion

At BeginningInsert a new node at the start of a linked list.O (1)O (1)
At the EndInsert a new node at the end of the linked list.O (N)O (1)
At Specific PositionInsert a new node at a specific position in a linked list.O (N)O (1)

Deletion

From BeginningDelete a node from the start of a linked listO (1)O (1)
From the EndDelete a node at the end of a linked list.O (N)O (1)
A Specific NodeDelete a node from a specific position of a linked list. O (N)O (1)

Traversal

Traverse the linked list from start to end or vice versa.O (N)O (1)

Circular Linked List

A circular linked list is a variation of a singly linked list where the last node points back to the first node, forming a circle. This means there is no NULL at the end, and the list can be traversed in a circular manner.

Circular-linked-list-in-c
Circular Linked List

The structure of the circular linked list node is same as that of singly linked list.

Representation of Circular Linked List

A circular linked list is represented as a pointer to the first node, where each node contains:

  • Data: The actual information stored in the node.
  • Next: A pointer that links to the next node, with the last node pointing back to the first node.
C
// Structure to represent the circular linked list
struct Node {
  
  // Data field - can be of any type and count
  int data;
  
  // Pointer to the next node
  struct Node* next;
}

Note: A circular linked list can also be represented by a pointer to the last node.

Basic Operations on Circular Linked List

Operation

Operation TypeDescriptionTime ComplexitySpace Complexity

Insertion

At BeginningInsert a new node at the start of a linked list.O (N)O (1)
At the EndInsert a new node at the end of the linked list.O (N)O (1)
At Specific PositionInsert a new node at a specific position in a linked list.O (N)O (1)

Deletion

From BeginningDelete a node from the start of a linked listO (N)O (1)
From the EndDelete a node at the end of a linked list.O (N)O (1)
A Specific NodeDelete a node from a specific position of a linked list. O (N)O (1)

Traversal

Traverse the linked list from start to end or vice versa.O (N)O (1)

Applications of Linked Lists

The following are some major applications of linked list:

  • Dynamic memory allocation efficiently manages and allocates dynamic memory in systems and applications.
  • Implementing other data structures such as stacks, queues, etc.
  • Represents and manipulates polynomials, with each node storing terms.
  • Used in file system management dynamically in operating systems.

Advantages of a Linked List

The following points shows the advantages of linked list:

  • Linked lists can grow or shrink in size dynamically, as memory is allocated or deallocated as needed.
  • Inserting or deleting nodes in a linked list is efficient and does not require shifting elements, unlike arrays.
  • Memory is utilized more efficiently as linked lists do not require a pre-allocated size, reducing wasted space.
  • They serve as the foundation for implementing more complex data structures like stacks, queues, and graphs.
  • They can utilize non-contiguous memory blocks, making them suitable for applications where memory is fragmented.

Disadvantages of a Linked List

Disadvantages of linked list are mentioned below:

  • Each node requires extra memory for storing a pointer.
  • Linked lists do not allow direct access to elements by index. Accessing a specific node requires traversing from the head, leading to slower access times.
  • Managing pointers can be tricky, increasing the complexity of coding.
  • Searching for an element or accessing a node by index takes O(N) time, making linked lists slower for such operations compared to arrays.
  • Non-contiguous memory allocation results in more cache misses.
  • Singly linked lists do not support easy backward traversal.

Next Article

Similar Reads