LINKED LIST

This is a linear data structure in which elements, known as nodes, are connected in a sequence, with each node containing data and a reference (or link) to the next node in the list. Unlike arrays, linked lists do not store elements in contiguous memory locations. Instead, each node can be dynamically allocated, making linked lists more flexible in memory usage. This structure allows for efficient insertions and deletions, as only the links between nodes need to be adjusted. However, accessing elements in a linked list requires traversal from the head node, making random access less efficient compared to arrays.

Despite their inefficiencies in random access, linked lists have practical uses in scenarios where frequent insertion and deletion operations are required. For instance, they are ideal for implementing dynamic data structures like stacks, queues, and associative arrays (hash tables). They are also used in real-time applications where memory allocation and deallocation need to be more flexible, such as in operating systems for managing processes or memory blocks. Additionally, linked lists are commonly used in implementing adjacency lists for graph representations, where the number of connections may vary and dynamic memory allocation is crucial.

PRIMARY OPERATIONS

  • Insertion: Inserting an element at end/beginning or in the middle at some kth position.

  • Deletion : Deleting an element from the list.

  • Display : Traversing the whole linked list and output each element.

CONCEPT

IMPLEMENTATION: EXAMPLE

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

// Define a struct for the linked list node
struct Node {
    int value;
    struct Node* next;
};

int main() {
    // Creating head of the Linked list
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head->value = 1;
    head->next = NULL;
    printf("The value at head is %d\n", head->value);
    // Don’t forget to free memory allocated for the head node
    free(head);
    return 0;
}

Last updated