📖 Scenario: Imagine you want to create a playlist of songs that plays in a loop. When the last song finishes, it starts again from the first song without stopping. This is like a circular list where the last item points back to the first.
🎯 Goal: You will build a circular singly linked list in C# that holds three songs. Each node will point to the next song, and the last song will point back to the first, creating a loop.
📋 What You'll Learn
Create a Node class with a string Data and a Next pointer.
Create three nodes with exact song names: "Song A", "Song B", and "Song C".
Link the nodes so that the last node points back to the first node, making the list circular.
Print the data of each node in the circular list exactly once, starting from the first node.
💡 Why This Matters
🌍 Real World
Circular linked lists are used in real-world applications like music playlists, round-robin scheduling, and buffering where the data repeats in a loop.
💼 Career
Understanding circular linked lists helps in software development roles that involve data structure design, game development, and systems programming.
Progress0 / 4 steps
1
Create the Node class and three nodes
Create a class called Node with a public string Data and a public Node reference called Next. Then create three Node objects named node1, node2, and node3 with Data values "Song A", "Song B", and "Song C" respectively.
DSA C#
Hint
Start by defining the Node class with two public members: Data and Next. Then create three nodes with the exact song names.
2
Link the nodes to form a circular list
Link the nodes by setting node1.Next to node2, node2.Next to node3, and node3.Next back to node1 to make the list circular.
DSA C#
Hint
Set each node's Next to the following node. For the last node, set Next back to the first node.
3
Create a variable to start traversal
Create a variable called current and set it to node1 to start traversing the circular list from the first node.
DSA C#
Hint
Set current to node1 so you can start printing from the first song.
4
Print all nodes in the circular list once
Use a do-while loop to print the Data of each node starting from current. Print each song exactly once by stopping when you reach current again after the first print.
DSA C#
Hint
Use a do-while loop to print the current node's data, then move to the next node. Stop when you return to the first node.
Practice
(1/5)
1. What is the key difference between a circular singly linked list and a regular singly linked list?
easy
A. The last node points to null in a circular singly linked list.
B. Nodes have two pointers in a circular singly linked list.
C. The last node points back to the first node in a circular singly linked list.
D. Nodes are stored in an array in a circular singly linked list.
Solution
Step 1: Understand the node connection in a regular singly linked list
In a regular singly linked list, the last node's next pointer is null, marking the end.
Step 2: Understand the node connection in a circular singly linked list
In a circular singly linked list, the last node's next pointer points back to the first node, forming a circle.
Final Answer:
The last node points back to the first node in a circular singly linked list. -> Option C
Quick Check:
Last node next = first node in circular list [OK]
Hint: Remember: circular means last node loops to first [OK]
Common Mistakes:
Thinking last node points to null in circular list
Confusing circular with doubly linked list
Assuming nodes have two pointers in circular singly linked list
2. Which of the following is the correct way to initialize a circular singly linked list with one node in C#?
easy
A. Node head = new Node(5); head.next = head;
B. Node head = new Node(5); head.next = null;
C. Node head = null; head.next = head;
D. Node head = new Node(5); head = null;
Solution
Step 1: Create a new node with value 5
Node head = new Node(5); creates a node with data 5.
Step 2: Point the node's next to itself to form a circular link
head.next = head; makes the list circular with one node.
Final Answer:
Node head = new Node(5); head.next = head; -> Option A
Quick Check:
Single node next points to itself [OK]
Hint: Single node circular list points next to itself [OK]
Common Mistakes:
Setting next to null instead of self
Assigning null to head after creation
Trying to set next on a null head
3. What will be the output of the following code snippet that creates a circular singly linked list with nodes 1, 2, 3 and prints them once?
class Node {
public int data;
public Node next;
public Node(int d) { data = d; next = null; }
}
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.next = third;
third.next = head; // circular link
Node temp = head;
do {
Console.Write(temp.data + " -> ");
temp = temp.next;
} while (temp != head);
Console.WriteLine("null");
medium
A. 1 -> 2 -> null
B. 1 -> 2 -> 3 -> 1 -> 2 -> 3 -> null
C. 1 -> 2 -> 3 -> 3 -> null
D. 1 -> 2 -> 3 -> null
Solution
Step 1: Create nodes and link them circularly
Nodes 1, 2, 3 are linked so that third.next points back to head, forming a circle.
Step 2: Traverse and print nodes once using do-while loop
The loop prints data starting at head and stops when temp returns to head, printing 1, 2, 3 once.
Final Answer:
1 -> 2 -> 3 -> null -> Option D
Quick Check:
Loop prints each node once before stopping [OK]
Hint: do-while stops after one full circle [OK]
Common Mistakes:
Using while loop without do causes skipping first node
Printing nodes infinitely without stop condition
Confusing circular list with linear list output
4. Identify the error in this code snippet that tries to insert a new node with value 4 at the end of a circular singly linked list:
B. The while loop condition should check for temp.next != head, not null.
C. temp should be initialized to null before the loop.
D. The newNode should be created after the loop.
Solution
Step 1: Understand circular list's last node pointer
In a circular singly linked list, the last node's next points to head, never null.
Step 2: Correct the loop condition to find last node
The loop should check temp.next != head to stop at the last node, not temp.next != null.
Final Answer:
The while loop condition should check for temp.next != head, not null. -> Option B
Quick Check:
Last node next = head, not null [OK]
Hint: In circular list, last node points to head, not null [OK]
Common Mistakes:
Using null check in circular list traversal
Setting newNode.next to null breaking circularity
Initializing temp incorrectly
5. You want to create a circular singly linked list from an array of integers [10, 20, 30, 40]. Which approach correctly creates the circular list and returns the head node?
public Node CreateCircularList(int[] arr) {
if (arr.Length == 0) return null;
Node head = new Node(arr[0]);
Node current = head;
for (int i = 1; i < arr.Length; i++) {
current.next = new Node(arr[i]);
current = current.next;
}
// What should be here?
}
hard
A. current.next = head; return head;
B. current.next = null; return head;
C. head.next = current; return current;
D. return current;
Solution
Step 1: Create nodes from array and link linearly
Loop creates nodes for each array element and links them in order.
Step 2: Make the list circular by linking last node to head
Setting current.next = head closes the loop, making the list circular.
Step 3: Return the head node
Return head to provide access to the circular list.
Final Answer:
current.next = head; return head; -> Option A
Quick Check:
Last node points to head to form circle [OK]
Hint: Close circle by linking last node to head [OK]
Common Mistakes:
Leaving last node next as null
Returning last node instead of head
Linking head.next to last node instead of last.next to head
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.