Lesson 5 +30 XP

General Lists, List ADT & Singly Linked Lists

General Lists, List ADT & Singly Linked Lists

A General List is a finite ordered sequence (a0, a1, …, an-1). Unlike arrays, Linked Lists allocate nodes dynamically on the heap (or from an AVAIL free-node pool) and chain them using pointers.

Linked List VariantsLinked List Variants

1. Array vs Linked List Comparison

FeatureArrayLinked List
Memory AllocationContiguous, fixed size (static)Non-contiguous, dynamic (malloc)
Random AccessO(1) via index arithmeticO(n) sequential traversal from HEAD
Insert / Delete at FrontO(n) (element shifting)O(1) (pointer update)
Extra MemoryNonePointer field(s) per node
Cache LocalityHigh (contiguous RAM)Lower (scattered heap nodes)

2. Singly Linked List (SLL) Manipulations

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

struct Node* insertFront(struct Node *head, int val) {
    struct Node *n = (struct Node*)malloc(sizeof(struct Node));
    n->data = val;
    n->next = head;
    return n; // new head
}

struct Node* deleteByKey(struct Node *head, int key) {
    struct Node *curr = head, *prev = NULL;
    while (curr != NULL && curr->data != key) {
        prev = curr;
        curr = curr->next;
    }
    if (!curr) return head; // not found
    if (!prev) head = curr->next;
    else prev->next = curr->next;
    free(curr);
    return head;
}

All Insertion & Deletion Cases

  • Insert at Beginning: newNode->next = head; head = newNode; - O(1).
  • Insert at End: Traverse until ptr->next == NULL, then ptr->next = newNode; - O(n) (or O(1) with a tail pointer).
  • Insert After Node prev: newNode->next = prev->next; prev->next = newNode; - O(1).
  • Insert in Sorted List: Walk curr while curr != NULL && curr->data < val keeping prev, then link between prev and curr.
  • Delete First / Last / By Key: Update head or prev->next, then free(curr) (returning the node to the heap or AVAIL list).

3. Header Linked Lists & AVAIL List

  • Header Linked List: Always keeps a sentinel header node at the start (often storing node count or max value) so head is never NULL:
  • Grounded Header List: Last node's next is NULL.
  • Circular Header List: Last node's next points back to the header node.
  • AVAIL (Free-Storage) List: Unused nodes are kept on a free list; allocation pops from AVAIL (Overflow if AVAIL == NULL), and deletion pushes the freed node back to AVAIL.

The List ADT and Its Array Implementation

List ADT operations: createList, insert(pos, x), delete(pos), retrieve(pos), search(x), traverse, isEmpty, isFull, listCount, destroyList.

Operation on an array of N itemsCostWhy
Print / traverseO(n)Visit every element
Find k-th elementO(1)Direct indexing
Insert at position 0O(n)Shift all N items right
Delete at position 0O(n)Shift all N items left
Insert / delete at endO(1)No shifting

Because insertion and deletion are expensive and the maximum size must be guessed in advance, a linked implementation is preferred for lists that change often.

Advantages and Disadvantages of Linked Lists

AdvantagesDisadvantages
Dynamic size-grows and shrinks at run timeExtra memory for the pointer in every node
Insertion / deletion only relink pointers, no shiftingNo random access-reaching the k-th node is O(n)
No memory wasted on unused capacityReverse traversal is hard in a singly linked list
Easy to build stacks, queues, graphs on topPoor cache locality (nodes scattered in memory)

Linked List Algorithms with START and AVAIL (Lipschutz style)

Here INFO[PTR] and LINK[PTR] are the fields of node PTR, START points to the first node and AVAIL is the list of free nodes.

Traverse / print

  1. Set PTR := START.
  2. Repeat steps 3-4 while PTR ≠ NULL.
  3. Apply PROCESS to INFO[PTR] (e.g., print it).
  4. Set PTR := LINK[PTR].
  5. Exit.

Search in an unsorted list (SEARCH(INFO, LINK, START, ITEM, LOC))

  1. Set PTR := START.
  2. Repeat while PTR ≠ NULL: if ITEM = INFO[PTR] then set LOC := PTR and Exit; else PTR := LINK[PTR].
  3. Set LOC := NULL (unsuccessful). Exit.

Search in a sorted list - same loop, but stop early: if ITEM < INFO[PTR] the item cannot appear later, so set LOC := NULL and Exit.

Insert at the beginning

  1. [Overflow?] If AVAIL = NULL, write OVERFLOW and Exit.
  2. Set NEW := AVAIL and AVAIL := LINK[AVAIL] (take a free node).
  3. Set INFO[NEW] := ITEM.
  4. Set LINK[NEW] := START.
  5. Set START := NEW.
  6. Exit.

Insert at the end - get NEW as above, set LINK[NEW] := NULL; if START = NULL set START := NEW, else walk PTR to the last node (LINK[PTR] = NULL) and set LINK[PTR] := NEW.

Insert after a given node LOC - get NEW; if LOC = NULL insert at the beginning; else set LINK[NEW] := LINK[LOC] and LINK[LOC] := NEW.

Insert before a given node - walk with two pointers SAVE (previous) and PTR until INFO[PTR] = VAL; then LINK[SAVE] := NEW and LINK[NEW] := PTR (if the node is the first, insert at the beginning).

Insert into a sorted list - find LOC, the last node with INFO < ITEM (walk SAVE/PTR while INFO[PTR] < ITEM), then "insert after LOC".

Delete the first node

  1. If START = NULL, write UNDERFLOW and Exit.
  2. Set PTR := START and START := LINK[START].
  3. Return PTR to AVAIL: LINK[PTR] := AVAIL, AVAIL := PTR.
  4. Exit.

Delete the last node - walk SAVE/PTR until LINK[PTR] = NULL, set LINK[SAVE] := NULL (or START := NULL if only one node), free PTR.

Delete the node after a given node LOC - set TEMP := LINK[LOC], LINK[LOC] := LINK[TEMP], free TEMP.

Delete ITEM from a sorted list - walk SAVE/PTR while INFO[PTR] < ITEM; if INFO[PTR] = ITEM, set LINK[SAVE] := LINK[PTR] (or START := LINK[START] for the first node) and free PTR; otherwise ITEM is not in the list.

🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Compare Arrays and Linked Lists. Explain Singly Linked List insertion (beginning, end, after node) and deletion by key with C functions. (7.5 Marks)
  • Write: 5-row comparison table (memory, access, front insert/delete, extra pointer, cache), pointer diagrams, and clean insertFront / deleteByKey C code.
  • Q2. What is a Header Linked List? Differentiate between Grounded and Circular Header Linked Lists. (5 Marks)
  • Write: Sentinel header node role (stores count/metadata, avoids head == NULL), Grounded (last->next = NULL) vs Circular (last->next = header).