BIT201 ยท 2080 ยท 5 marks
Write short notes on a.) Doubly circular linked list Write short notes on b.) Breadth first Search [2.5+2.5]
Write short notes on a.) Doubly circular linked list Write short notes on b.) Breadth first Search [2.5+2.5]
Data Structures and AlgorithmsChapter 4: Linked ListsDoubly circular linked list operations
Quick answer
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.
Short Notes
a.) Doubly 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.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.