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.
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]
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 answerHide 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
- Start from the source node, mark it as visited, and enqueue it.
- Dequeue a node from the front of the queue.
- Visit all unvisited adjacent nodes, mark them visited, and enqueue them.
- 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:
| Step | Dequeue | Queue After | Visited |
|---|---|---|---|
| 1 | 1 | [2, 3] | 1 |
| 2 | 2 | [3, 4, 5] | 1,2,3 |
| 3 | 3 | [4, 5] | 1,2,3 |
| 4 | 4 | [5] | 1,2,3,4 |
| 5 | 5 | [] | 1,2,3,4,5 |
Output: 1 -> 2 -> 3 -> 4 -> 5
Characteristics
| Property | Value |
|---|---|
| Data Structure Used | Queue |
| Time Complexity | O(V + E) |
| Space Complexity | O(V) |
| Traversal Order | Level 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.
Singly linked list definition and traversal
2079 · 5 marks
Explain singly linked list with example. Compare singly linked list with doubly linked list. [5]
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]
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]
Make Unit 4 stick
Practice BIT201 with flashcards & quizzes