4 Linked Lists

Data Structures and Algorithms · Unit 4

Linked Lists

Exam-focused notes for Linked Lists (Data Structures and Algorithms, BIT201): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

Linked Lists in short

A linked list stores data in separate nodes joined by pointers. Unlike an array, it can grow and shrink easily and does not need continuous memory.

Linked Lists: notes

Singly linked list definition and traversal

A singly linked list is a chain of nodes. Each node has two parts: the data and a next pointer to the following node. A pointer called head holds the address of the first node. The next pointer of the last node is NULL.

head -> [10 | *] -> [20 | *] -> [30 | NULL]
struct Node {
    int data;
    struct Node *next;
};

Traversal means visiting every node once, starting from the head.

void traverse(struct Node *head) {
    struct Node *p = head;
    while (p != NULL) {
        printf("%d ", p->data);
        p = p->next;
    }
}

For the list above, it prints 10 20 30. Traversal takes $O(n)$ time. A singly linked list can only be traversed forward.

Doubly linked list definition and comparison

A doubly linked list has nodes with three parts: a prev pointer, the data, and a next pointer. It can be traversed forward and backward. The prev of the first node and the next of the last node are NULL.

NULL <- [10] <-> [20] <-> [30] -> NULL
struct DNode {
    int data;
    struct DNode *prev, *next;
};
Singly linked listDoubly linked list
One pointer per node (next)Two pointers per node (prev and next)
Traversal in one directionTraversal in both directions
Less memoryMore memory
Deleting a given node needs the previous nodeA given node can be deleted using its own prev
Simpler to codeMore pointer updates, so more chance of error

What this unit covers

  • Singly linked list definition and traversal
  • Doubly linked list definition and comparison
  • Circular linked list definition and traversal
  • Doubly circular linked list operations
  • Insertion at front position algorithm
  • Deletion of last node algorithm
  • Advantages and disadvantages of linked list over array

Doubly circular linked list operations

2082 · 5 marks

Explain the insertion and deletion of a node at first and last position of doubly circular linked list. [5]

Each node has three fields: PREV (pointer to previous node), DATA, and NEXT (pointer to next node). In a doubly circular linked list: - The last node's NEXT points to the first node - The first node's PREV points to the last node --- Steps: 1. Create a new node newNode. 2. If the list is empty (h...

Open this answer on its own page →

2080 · 5 marks

Write short notes on a.) Doubly circular linked list Write short notes on b.) Breadth first Search [2.5+2.5]

Show solved answer

Short Notes

a.) Doubly Circular Linked List

A Doubly Circular Linked List is a type of linked list that combines the properties of both a doubly linked list and a circular linked list.

Structure of Each Node

Each node contains three fields:

[ PREV | DATA | NEXT ]
  • PREV: Pointer to the previous node
  • DATA: The actual data stored
  • NEXT: Pointer to the next node

Key Properties

  • Every node has a pointer to both its next and previous node.
  • The last node's NEXT pointer points back to the first node.
  • The first node's PREV pointer points back to the last node.
  • There is no NULL pointer in the list.

Diagram

  +-------+    +-------+    +-------+
  |  10   | <->|  20   | <->|  30   |
  +-------+    +-------+    +-------+
      ^                          |
      |__________________________|

Advantages

  • Traversal is possible in both directions (forward and backward).
  • Insertion and deletion are easier compared to singly linked lists.
  • Starting from any node, the entire list can be traversed.
  • No need to handle NULL; traversal stops when we return to the starting node.

Disadvantages

  • Requires extra memory for the PREV pointer.
  • Implementation is more complex than singly linked lists.

Applications

  • Used in music players (next/previous song).
  • Used in browser history (forward/backward navigation).
  • Implementation of deques (double-ended queues).

b.) Breadth First Search (BFS)

Breadth First Search (BFS) is a graph/tree traversal algorithm that explores all nodes level by level, starting from a given source node.

Key Idea

  • Visit all neighbors of a node before moving to the next level.
  • Uses a Queue (FIFO) data structure to keep track of nodes to visit.
  • Uses a visited array to avoid revisiting nodes.

Algorithm Steps

  1. Start from the source node, mark it as visited, and enqueue it.
  2. Dequeue a node from the front of the queue.
  3. Visit all unvisited adjacent nodes, mark them visited, and enqueue them.
  4. Repeat steps 2-3 until the queue is empty.

Pseudocode

BFS(Graph, start):
    Create a Queue Q
    Mark start as visited
    Enqueue start into Q

    while Q is not empty:
        node = Dequeue(Q)
        print node
        for each neighbor of node:
            if neighbor is not visited:
                mark neighbor as visited
                Enqueue(neighbor)

Example

Consider the graph:

    1
   / \
  2   3
 / \
4   5

BFS Traversal starting from node 1:

StepDequeueQueue AfterVisited
11[2, 3]1
22[3, 4, 5]1,2,3
33[4, 5]1,2,3
44[5]1,2,3,4
55[]1,2,3,4,5

Output: 1 -> 2 -> 3 -> 4 -> 5

Characteristics

PropertyValue
Data Structure UsedQueue
Time ComplexityO(V + E)
Space ComplexityO(V)
Traversal OrderLevel by Level

Applications

  • Shortest path finding in unweighted graphs.
  • Web crawlers for indexing pages.
  • Social networking (finding friends within k distance).
  • Cycle detection in graphs.
Open this answer on its own page →

Singly linked list definition and traversal

2079 · 5 marks

Explain singly linked list with example. Compare singly linked list with doubly linked list. [5]

A singly linked list is a linear data structure in which elements (called nodes) are stored in non-contiguous memory locations. Each node contains two parts: 1. Data field - stores the actual data/value 2. Next pointer - stores the address (reference) of the next node in the sequence The last nod...

Open this answer on its own page →

Advantages and disadvantages of linked list over array

0 · 10 marks

What are the advantages and disadvantages of linked list over an array? Discuss algorithms for inserting a node at front position of the linked list and deleting its last item in singly linked list.[10]

--- Advantage Explanation --------------------------- 1 Dynamic Size Linked list can grow or shrink at runtime. No need to declare size in advance, unlike arrays which have a fixed size. 2 Efficient Insertion/Deletion Inserting or deleting a node (especially at front) takes O(1) time without shif...

Open this answer on its own page →

Circular linked list definition and traversal

0 · 5 marks

What is a circular linked list? How can you traverse all nodes in a singly linked list? [5]

--- A circular linked list is a linked list in which the last node does not contain a NULL pointer. Instead, the last node points back to the first (head) node, forming a circle or loop. There are two types: - Singly Circular Linked List: Each node has one next pointer, and the last node's next p...

Open this answer on its own page →