Linked List: An Efficient Linear Data Structure


Linked lists are widely used linear data structures that consist of a sequence of nodes. Unlike arrays, linked lists do not store elements at contiguous memory locations. Instead, the elements can be stored anywhere in memory, connected to each other using pointers. In this post, we will explore the characteristics, advantages, and disadvantages of linked lists, shedding light on their dynamic nature and various applications.


Understanding Linked Lists:

Linked lists provide a flexible way to organize and manipulate data. Each node in a linked list contains its own data and a pointer/reference to the next node in the sequence. This connectivity allows for dynamic allocation of memory, as nodes can be inserted or removed from the list effortlessly. The absence of a fixed memory block allows linked lists to adapt to changing data requirements efficiently.




                                   File:C language linked list.png - Wikimedia Commons





Advantages of Linked Lists over Arrays:

While arrays can store linear data of similar types, linked lists offer distinct advantages:

  1. Dynamic Size: Linked lists have a dynamic nature, allocating memory as needed. This flexibility allows for efficient memory utilization, especially in scenarios where the number of elements may vary.

  2. Insertion and Deletion Efficiency: Inserting or deleting a node in a linked list involves updating pointers, resulting in faster operations compared to arrays. Arrays require shifting elements, which can be expensive in terms of time complexity.

  3. Execution of Stacks and Queues: Linked lists provide a convenient foundation for implementing stack and queue data structures, making it easier to execute related operations.

  4. Reduced Access Time: Traversing a linked list sequentially is generally faster than accessing random elements in an array. This advantage can be significant in scenarios where sequential access is the primary requirement.




Disadvantages of Linked Lists:

Despite their advantages, linked lists have a few limitations:

  1. Pointer Overhead: Linked lists require additional memory for storing pointers, leading to slightly higher memory consumption compared to arrays.

  2. Sequential Access: To access a specific element, linked lists must traverse each node sequentially from the beginning. Random access to elements is not possible, which can be a disadvantage in certain scenarios.

  3. Reverse Traversal Complexity: Reversing the order of elements in a linked list is more challenging compared to arrays, as backward traversal requires additional operations.

  4. Lack of Cache Locality: Unlike arrays, where elements are stored contiguously, linked lists do not exhibit locality of reference. This can affect cache performance in certain situations.




Representation of Linked Lists

The representation of a linked list involves a pointer that points to the first node in the list, which is called the head node. When the linked list is empty, the head node has a value of NULL.

Each node in a linked list comprises two essential components: the data it holds and a pointer that indicates the location of the next node in the sequence.



In programming languages like C/C++, a node can be represented using a structure. Consider the following example of a linked list node that stores integer data:


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


Node class in Java for a linked list:

class Node {
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}






sample program in Java that creates a simple linked list with three nodes:


class Node {
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class LinkedListExample {
    public static void main(String[] args) {
        // Creating three nodes
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);

        // Linking the nodes
        node1.next = node2;
        node2.next = node3;

        // Displaying the linked list
        Node current = node1;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }
}


In this program, we have a Node class that represents each node in the linked list. Each node has an int data field and a reference to the next node (next).

In the LinkedListExample class, we create three node objects node1, node2, and node3 with data values 1, 2, and 3, respectively.

To link these nodes together, we set the next reference of each node to the next node in the sequence. In this case, we link node1 to node2 and node2 to node3.

Finally, we iterate through the linked list, starting from node1, and print the data values of each node.

The output of this program will be:

1 2 3




 sample program in C++ to create a simple linked list with three nodes:

#include <iostream>

struct Node {
    int data;
    Node* next;

    Node(int data) {
        this->data = data;
        this->next = nullptr;
    }
};

int main() {
    // Creating three nodes
    Node* node1 = new Node(1);
    Node* node2 = new Node(2);
    Node* node3 = new Node(3);

    // Linking the nodes
    node1->next = node2;
    node2->next = node3;

    // Displaying the linked list
    Node* current = node1;
    while (current != nullptr) {
        std::cout << current->data << " ";
        current = current->next;
    }

    return 0;
}






No comments

darkmode