Loading lessons...
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 Variants
1. Array vs Linked List Comparison
| Feature | Array | Linked List |
|---|---|---|
| Memory Allocation | Contiguous, fixed size (static) | Non-contiguous, dynamic (malloc) |
| Random Access | O(1) via index arithmetic | O(n) sequential traversal from HEAD |
| Insert / Delete at Front | O(n) (element shifting) | O(1) (pointer update) |
| Extra Memory | None | Pointer field(s) per node |
| Cache Locality | High (contiguous RAM) | Lower (scattered heap nodes) |
2. Singly Linked List (SLL) Manipulations
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, thenptr->next = newNode;- O(n) (or O(1) with atailpointer). - Insert After Node
prev:newNode->next = prev->next; prev->next = newNode;- O(1). - Insert in Sorted List: Walk
currwhilecurr != NULL && curr->data < valkeepingprev, then link betweenprevandcurr. - Delete First / Last / By Key: Update
headorprev->next, thenfree(curr)(returning the node to the heap orAVAILlist).
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
headis neverNULL: - Grounded Header List: Last node's
nextisNULL. - Circular Header List: Last node's
nextpoints back to the header node. AVAIL(Free-Storage) List: Unused nodes are kept on a free list; allocation pops fromAVAIL(Overflow ifAVAIL == NULL), and deletion pushes the freed node back toAVAIL.
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 items | Cost | Why |
|---|---|---|
| Print / traverse | O(n) | Visit every element |
| Find k-th element | O(1) | Direct indexing |
| Insert at position 0 | O(n) | Shift all N items right |
| Delete at position 0 | O(n) | Shift all N items left |
| Insert / delete at end | O(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
| Advantages | Disadvantages |
|---|---|
| Dynamic size-grows and shrinks at run time | Extra memory for the pointer in every node |
| Insertion / deletion only relink pointers, no shifting | No random access-reaching the k-th node is O(n) |
| No memory wasted on unused capacity | Reverse traversal is hard in a singly linked list |
| Easy to build stacks, queues, graphs on top | Poor 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
- Set PTR := START.
- Repeat steps 3-4 while PTR ≠ NULL.
- Apply PROCESS to INFO[PTR] (e.g., print it).
- Set PTR := LINK[PTR].
- Exit.
Search in an unsorted list (SEARCH(INFO, LINK, START, ITEM, LOC))
- Set PTR := START.
- Repeat while PTR ≠ NULL: if ITEM = INFO[PTR] then set LOC := PTR and Exit; else PTR := LINK[PTR].
- 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
- [Overflow?] If AVAIL = NULL, write OVERFLOW and Exit.
- Set NEW := AVAIL and AVAIL := LINK[AVAIL] (take a free node).
- Set INFO[NEW] := ITEM.
- Set LINK[NEW] := START.
- Set START := NEW.
- 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
- If START = NULL, write UNDERFLOW and Exit.
- Set PTR := START and START := LINK[START].
- Return PTR to AVAIL: LINK[PTR] := AVAIL, AVAIL := PTR.
- 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.
📝 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/deleteByKeyC 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).