Traversal Forward and Backward in a Doubly Linked List
📖 Scenario: You are building a simple contact list app. The contacts are stored in a doubly linked list so you can move forward and backward through the list easily.
🎯 Goal: Create a doubly linked list with three contacts. Then, traverse the list forward to print all contacts, and traverse backward to print all contacts in reverse order.
📋 What You'll Learn
Create a doubly linked list node struct with name, prev, and next fields
Create three nodes with names exactly "Alice", "Bob", and "Charlie"
Link the nodes to form a doubly linked list
Traverse the list forward starting from the head node and collect the names
Traverse the list backward starting from the tail node and collect the names
Print the forward traversal result as a string with names separated by " -> "
Print the backward traversal result as a string with names separated by " -> "
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in applications like music players to move forward and backward through songs, or in browsers to navigate forward and backward through pages.
💼 Career
Understanding doubly linked lists helps in jobs involving system programming, game development, and any software that requires efficient two-way navigation through data.
Progress0 / 4 steps
1
Create the doubly linked list nodes
Create a struct called Node with fields name (string), prev (*Node), and next (*Node). Then create three variables called node1, node2, and node3 of type Node with names "Alice", "Bob", and "Charlie" respectively.
DSA Go
Hint
Define the struct first, then create three variables of that struct type with the exact names.
2
Link the nodes to form a doubly linked list
Link the nodes so that node1.next points to &node2, node2.prev points to &node1, node2.next points to &node3, and node3.prev points to &node2. Also create two variables called head and tail that point to &node1 and &node3 respectively.
DSA Go
Hint
Set the next and prev pointers to link the nodes forward and backward. Then assign head and tail to the first and last nodes.
3
Traverse forward and backward to collect names
Create two slices of strings called forwardNames and backwardNames. Traverse the list forward starting from head and append each node's name to forwardNames. Then traverse backward starting from tail and append each node's name to backwardNames.
DSA Go
Hint
Use a for loop to move forward using next pointers and backward using prev pointers, appending names to slices.
4
Print the forward and backward traversal results
Use fmt.Println to print the forward traversal names joined by " -> " as Forward: Alice -> Bob -> Charlie. Then print the backward traversal names joined by " -> " as Backward: Charlie -> Bob -> Alice. Import fmt and strings packages.
DSA Go
Hint
Use strings.Join(slice, " -> ") to join names and fmt.Println to print the results.
Practice
(1/5)
1. What does "Traversal Forward and Backward" mean in the context of a doubly linked list?
easy
A. Visiting nodes from the start to the end and then from the end back to the start
B. Only visiting nodes from the start to the end
C. Only visiting nodes from the end to the start
D. Skipping nodes randomly during traversal
Solution
Step 1: Understand forward traversal
Forward traversal means visiting nodes starting from the first node and moving to the last node.
Step 2: Understand backward traversal
Backward traversal means visiting nodes starting from the last node and moving back to the first node.
Final Answer:
Visiting nodes from the start to the end and then from the end back to the start -> Option A
Quick Check:
Traversal forward and backward = visiting nodes both ways [OK]
Hint: Think of walking forward then turning back [OK]
Common Mistakes:
Confusing forward traversal with backward traversal
Thinking traversal only goes one way
Assuming traversal skips nodes
2. Which of the following Go code snippets correctly traverses a doubly linked list forward?
easy
A. for node := head; node != nil; node = node.prev { fmt.Println(node.value) }
B. for node := head; node != nil; node = node.next { fmt.Println(node.value) }
C. for node := tail; node != nil; node = node.next { fmt.Println(node.value) }
D. for node := tail; node != nil; node = node.prev { fmt.Println(node.value) }
Solution
Step 1: Identify forward traversal start point
Forward traversal starts at the head (first node).
Step 2: Identify correct pointer for forward traversal
Moving forward uses the next pointer, so node = node.next is correct.
Final Answer:
for node := head; node != nil; node = node.next { fmt.Println(node.value) } -> Option B
Quick Check:
Forward traversal uses head and next pointer [OK]
Hint: Forward traversal starts at head and uses next [OK]
Common Mistakes:
Starting traversal at tail for forward direction
Using prev pointer for forward traversal
Confusing next and prev pointers
3. What is the output of the following Go code snippet that traverses a doubly linked list backward?