Home » Data Structure » Singly Linked List, Its Advantage, Disadvantage and Application

Singly Linked List, Its Advantage, Disadvantage and Application

Last updated:

Support this post with a reaction:

2,656 views

A Singly Linked List is a linear data structure in which elements are stored in separate memory locations called nodes. Each node contains two parts: a data field that stores the actual value and a next pointer that stores the address of the next node.

Unlike arrays, linked list elements are not stored in contiguous memory locations. Each node is connected to the next node using pointers, forming a sequence of elements.

A singly linked list starts with a pointer called HEAD, which points to the first node. The next pointer of the last node contains NULL, indicating the end of the list.

Structure of a Singly Linked List

A singly linked list consists of nodes connected through pointers.

Each node has two main components:

  1. Data: Stores the actual information or value.
  2. Next Pointer: Stores the address of the next node in the linked list.

The basic structure of a node in C is:

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

Here:

  • data stores the value of the node.
  • next stores the address of the next node.
  • NULL indicates that there is no next node.
Singly Linked List

Creation of a Singly Linked List

A linked list can be created by dynamically allocating nodes and linking them together.

C Program to create Singly Linked List


#include <stdio.h>
#include <stdlib.h>

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

int main() {

    struct Node *head = NULL;

    struct Node *first = malloc(sizeof(struct Node));
    struct Node *second = malloc(sizeof(struct Node));

    // Check memory allocation
    if (first == NULL || second == NULL) {
        free(first);
        free(second);
        printf("Memory allocation failed!");
        return 1;
    }

    // Initialize first node
    first->data = 10;
    first->next = second;

    // Initialize second node
    second->data = 20;
    second->next = NULL;

    // Assign head pointer
    head = first;

    // Traverse and display linked list
    struct Node *temp = head;

    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }

    printf("NULL");

    // Free allocated memory
    free(first);
    free(second);

    return 0;
}

Output

10 -> 20 -> NULL

Working of Singly Linked List

A singly linked list works by connecting individual nodes using pointers. Each node maintains the address of the next node, allowing the program to move through the list sequentially.

The working process involves:

  1. Head Pointer: Stores the address of the first node.
  2. Node Creation: Memory is dynamically allocated for each new node.
  3. Data Storage: The value is stored in the data field.
  4. Pointer Connection: The next pointer connects the current node to the following node.
  5. NULL Pointer: The last node contains NULL to indicate the end of the list.

Singly Linked List Traversal

Traversal means visiting every node of a linked list one by one, starting from the HEAD pointer.

For example, if the linked list contains 10, 20 and 30, traversal produces:

10 -> 20 -> 30 -> NULL

The traversal process continues until the current pointer becomes NULL.

Traversal Algorithm for Singly Linked List

  1. Start from the HEAD pointer.
  2. Check whether HEAD is NULL.
  3. If HEAD is NULL, the list is empty.
  4. Otherwise, display the data of the current node.
  5. Move to the next node using the next pointer.
  6. Repeat until the current pointer becomes NULL.

Operations on Singly Linked Lists

1. Traversal in single Singly Linked Lists

Traversal is the process of visiting each node from the first node to the last node.

Traversal Algorithm of Singly Linked Lists

TRAVERSE(START)

1. PTR = START
2. WHILE PTR != NULL
3.     PRINT PTR->DATA
4.     PTR = PTR->NEXT
5. END WHILE
void traverse(struct Node *head) {
    struct Node *temp = head;

    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}

Time complexity: O(n).

2. Insertion at the Beginning in Singly Linked List

Insertion at the beginning adds a new node before the current first node.

Algorithm of Insertion at the Beginning in Singly Linked List

INSERT_BEGIN(START, ITEM)

1. Create NEW node
2. NEW->DATA = ITEM
3. NEW->NEXT = START
4. START = NEW
5. RETURN START
struct Node *insertBegin(struct Node *head, int item) {
    struct Node *newNode =
        malloc(sizeof(struct Node));

    newNode->data = item;
    newNode->next = head;

    return newNode;
}

Time complexity: O(1).

3. Insertion at the End in Singly Linked List

Insertion at the end adds a node after the last node.

Algorithm of Insertion at the End in Singly Linked List

struct Node *insertEnd(struct Node *head, int item) {
    struct Node *newNode =
        malloc(sizeof(struct Node));

    newNode->data = item;
    newNode->next = NULL;

    if (head == NULL)
        return newNode;

    struct Node *temp = head;

    while (temp->next != NULL)
        temp = temp->next;

    temp->next = newNode;

    return head;
}

Time complexity:

  • O(n) without a tail pointer.
  • O(1) if a tail pointer is maintained.

4. Insertion After a Given Node in Singly Linked List

A new node is inserted after a specified node.

Example:

10 → 20 → 40

Insert 30 after 20.

Result:

10 → 20 → 30 → 40

Algorithm of Insertion After a Given Node in Singly Linked List

INSERT_AFTER(PTR, ITEM)

1. IF PTR = NULL
       RETURN
2. Create NEW node
3. NEW->DATA = ITEM
4. NEW->NEXT = PTR->NEXT
5. PTR->NEXT = NEW
void insertAfter(struct Node *ptr, int item) {
    if (ptr == NULL)
        return;

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

    newNode->data = item;
    newNode->next = ptr->next;

    ptr->next = newNode;
}

Time complexity: O(1), when the specified node is already available.

5. Deletion at the Beginning in Singly Linked List

Deletion at the beginning removes the first node.

Algorithm Deletion at the Beginning in Singly Linked List:

DELETE_BEGIN(START)

1. IF START = NULL
       PRINT UNDERFLOW
       RETURN
2. TEMP = START
3. START = START->NEXT
4. FREE TEMP
5. RETURN START
struct Node *deleteBegin(struct Node *head) {
    if (head == NULL)
        return NULL;

    struct Node *temp = head;

    head = head->next;

    free(temp);

    return head;
}

Time complexity: O(1).

6. Deletion at the End in Singly Linked List

Deletion at the end removes the last node.

Algorithm of Deletion at the End in Singly Linked List:

DELETE_END(START)

1. IF START = NULL
       RETURN
2. IF START->NEXT = NULL
       FREE START
       START = NULL
       RETURN
3. PTR = START
4. WHILE PTR->NEXT->NEXT != NULL
       PTR = PTR->NEXT
5. TEMP = PTR->NEXT
6. PTR->NEXT = NULL
7. FREE TEMP
struct Node *deleteEnd(struct Node *head) {
    if (head == NULL)
        return NULL;

    if (head->next == NULL) {
        free(head);
        return NULL;
    }

    struct Node *temp = head;

    while (temp->next->next != NULL)
        temp = temp->next;

    free(temp->next);
    temp->next = NULL;

    return head;
}

Time complexity: O(n).

7. Deletion of a Specific Node in Singly Linked List

A node containing a specified value is removed from the list.

Example:

10 → 20 → 30 → 40

Delete 30.

Result:

10 → 20 → 40

struct Node *deleteValue(struct Node *head, int key) {
    if (head == NULL)
        return NULL;

    if (head->data == key) {
        struct Node *temp = head;
        head = head->next;
        free(temp);
        return head;
    }

    struct Node *temp = head;

    while (temp->next != NULL &&
           temp->next->data != key) {
        temp = temp->next;
    }

    if (temp->next != NULL) {
        struct Node *del = temp->next;
        temp->next = del->next;
        free(del);
    }

    return head;
}

Time complexity: O(n)

Advantages of a Singly Linked List

The important advantages of a singly linked list are:

  1. Dynamic Memory Allocation: A linked list can grow or shrink during program execution without requiring a fixed size in advance.
  2. Efficient Insertion: New nodes can be inserted by updating pointers without shifting existing elements.
  3. Efficient Deletion: Nodes can be removed by changing pointer connections instead of shifting elements.
  4. Flexible Memory Utilization: Memory is allocated when new nodes are created, avoiding the need to reserve a fixed-size array in advance.
  5. Easy Implementation of Other Data Structures: Singly linked lists can be used to implement stacks, queues and other dynamic structures.
  6. No Contiguous Memory Requirement: Nodes can be stored at different memory locations and connected through pointers.

Disadvantages of a Singly Linked List

The major disadvantages of a singly linked list are:

  1. No Random Access: Elements cannot be accessed directly using an index. Traversal must start from the first node.
  2. Extra Memory Requirement: Every node requires additional memory to store the next pointer.
  3. Sequential Traversal: Nodes can only be accessed in the forward direction.
  4. Complex Pointer Management: Insertion and deletion operations require careful pointer handling.
  5. Poor Cache Performance: Nodes may be located at different memory addresses, which can reduce cache efficiency compared with arrays.
  6. No Backward Traversal: A singly linked list cannot move directly to the previous node.

Applications of a Singly Linked List

Singly linked lists are used in applications where dynamic data storage and sequential access are required.

  1. Stack Implementation: A singly linked list can implement a stack using insertion and deletion at the beginning.
  2. Queue Implementation: Linked lists can implement queues using front and rear pointers.
  3. Hash Table Collision Resolution: Linked lists are used in separate chaining to store multiple values in the same hash bucket.
  4. Graph Representation: Adjacency lists can use linked structures to store neighboring vertices.
  5. Polynomial Representation: Each node can store a coefficient and exponent to represent polynomial expressions.
  6. Dynamic Memory Management: Linked structures can help maintain lists of available or allocated memory blocks.
  7. Playlist Navigation: A singly linked list can represent a playlist that moves forward from one song to another.
  8. Sparse Matrix Representation: Linked structures can store non-zero matrix elements without allocating memory for every zero element.