Site icon NOTE BAHADUR

Linked List

A Linked List is a linear data structure where elements are not stored in adjacent or contiguous memory locations. Instead, elements are dynamically linked using memory pointers.

As an Abstract Data Type (ADT), a linked list defines a set of clear structural operations—such as insertion, deletion, and traversal—without specifying the exact lower-level implementation. This logical blueprint allows developers to isolate user-facing operations from complex internal pointer mechanics.

Dynamic Implementation and the Node Structure

Unlike standard arrays, which are static structures with a fixed size allocated at compile time, a linked list is completely dynamic, with memory allocated at runtime. This fluidity is achieved by encapsulating each individual data element within a construct called a Node.

A basic node in a Singly Linked List consists of two primary fields:

  1. Data Field: Stores the actual value, variable, or structural record.
  2. Next Pointer: Stores the exact memory address of the subsequent node in the sequence.
       Head
        |
        v
     +------+------+      +------+------+      +------+------+
     | Data | Next |----> | Data | Next |----> | Data | NULL |
     +------+------+      +------+------+      +------+------+

Node Manipulation: Insertion and Deletion Mechanics

Mastering a linked list requires a solid grasp of node manipulation across various arbitrary positions:

Advanced Variations: Linked Stacks, Queues, and Doubly Linked Lists

1. Linked Stacks and Linked Queues

Because linked lists grow dynamically at runtime, they are ideal for implementing other structural ADTs without the risk of “Stack Overflow” or “Queue Full” errors associated with fixed-capacity arrays.

2. Doubly Linked Lists (DLL)

A Doubly Linked List is a more robust structural variation where each node maintains two separate pointers: Next (targeting the subsequent node) and Prev (targeting the preceding node).

          Head
           |
           v
        +------+------+------+     +------+------+------+
 NULL <-| Prev | Data | Next |<--->| Prev | Data | Next |-> NULL
        +------+------+------+     +------+------+------+

Core Advantages of a Doubly Linked List:

Technical Architecture & Operational Logic

1. Singly Linked List: Insertion at the Beginning

To add a node at the start of a list, the Head pointer must be updated to target the new element:

  1. Allocate memory dynamically for the newNode.
  2. Set newNode->data = value.
  3. Set newNode->next = head (The new node now points to the pre-existing first node).
  4. Update head = newNode (The head pointer now safely redirects to our new node).

2. Singly Linked List: Deletion at the End

To remove the final node, the system must locate the second-to-last node to change its pointer to NULL:

  1. Traverse the list until reaching the node where current->next->next == NULL.
  2. Free the memory occupied by current->next (the old final node).
  3. Set current->next = NULL (the current node becomes the new tail).

3. Doubly Linked List: Insertion at the End

  1. Allocate memory dynamically for the newNode.
  2. Traverse to the current last node (where lastNode->next == NULL).
  3. Set lastNode->next = newNode.
  4. Set newNode->prev = lastNode.
  5. Set newNode->next = NULL.

Deep-Dive Comparative Analysis

Part A: Sizing and Allocation Performance

Attribute / FeatureStatic Array ListsDynamic Linked Lists
Memory AllocationFixed size at compile time; contiguous memory blocks.Dynamic size at runtime; non-contiguous memory slots.
Access VelocityFast: O(1) random access via index calculation.Slow: O(n) sequential access; must traverse node-by-node.
Modification CostHigh: O(n); requires shifting all trailing elements.Low: O(1) for pointer adjustments once the node is located.

Part B: Singly vs. Doubly Linked List Trade-offs

Attribute / FeatureSingly Linked ListDoubly Linked List
Pointer SchemeOne pointer (next) per node.Two pointers (next and prev) per node.
Traversal DirectionUnidirectional (forward navigation only).Bidirectional (seamless forward and backward navigation).
Memory UsageLower memory consumption per node.Higher memory consumption due to the secondary structural pointer.
Exit mobile version