📖 Scenario: You have a list of daily temperatures recorded in a week. You want to look at these temperatures both from the start to the end of the week and then from the end back to the start.
🎯 Goal: Build a program that stores the temperatures in a list, then prints them forward (start to end) and backward (end to start) using simple loops.
📋 What You'll Learn
Create a list of integers called temperatures with exact values for each day.
Create an integer variable called count to hold the number of temperature entries.
Use a for loop with variable i to traverse temperatures forward and print each temperature.
Use a for loop with variable j to traverse temperatures backward and print each temperature.
💡 Why This Matters
🌍 Real World
Looking at data forward and backward is common in time series analysis, like checking temperatures, stock prices, or sensor readings.
💼 Career
Understanding how to traverse data structures in both directions is a basic skill for software developers working with lists, arrays, or other collections.
Progress0 / 4 steps
1
Create the temperature list
Create a list of integers called temperatures with these exact values: 23, 25, 22, 20, 18, 21, 19.
DSA C#
Hint
Use List<int> and initialize it with the exact numbers inside curly braces.
2
Add a count variable
Create an integer variable called count and set it to the number of items in the temperatures list.
DSA C#
Hint
Use the Count property of the list to get the number of elements.
3
Traverse forward and backward
Use a for loop with variable i from 0 to count - 1 to print each temperature forward. Then use a for loop with variable j from count - 1 down to 0 to print each temperature backward.
DSA C#
Hint
Use two separate for loops: one counting up from 0, the other counting down from count - 1.
4
Print the traversal results
Run the program to print the temperatures forward and backward as two separate lists.
DSA C#
Hint
Check the console output matches the forward list first, then the backward list.
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. Visiting nodes randomly without order
D. Visiting nodes only from the end to the start
Solution
Step 1: Understand traversal directions
Traversal forward means visiting nodes from the first node to the last node. Traversal backward means visiting nodes from the last node back to the first node.
Step 2: Apply to doubly linked list
A doubly linked list allows moving in both directions because each node has links to the next and previous nodes.
Final Answer:
Visiting nodes from the start to the end and then from the end back to the start -> Option A
Quick Check:
Forward and backward traversal = visiting nodes both ways [OK]
Hint: Think of walking forward then turning back [OK]
Common Mistakes:
Confusing traversal with random access
Thinking traversal is only forward
Ignoring backward links in doubly linked list
2. Which of the following is the correct syntax to traverse a doubly linked list forward in C#?
easy
A. for(Node current = tail; current != null; current = current.Next) { /* process current */ }
B. for(Node current = head; current != null; current = current.Next) { /* process current */ }
C. for(Node current = head; current != null; current = current.Prev) { /* process current */ }
D. for(Node current = tail; current != null; current = current.Prev) { /* process current */ }
Solution
Step 1: Identify forward traversal
Forward traversal starts at the head and moves to the next node repeatedly until null.
Step 2: Check syntax correctness
for(Node current = head; current != null; current = current.Next) { /* process current */ } correctly starts at head and moves using current = current.Next. Other options either start at tail or move backward.
Final Answer:
for(Node current = head; current != null; current = current.Next) { /* process current */ } -> Option B
Quick Check:
Forward traversal uses Next pointer from head [OK]
Hint: Forward means start at head and use Next [OK]
Common Mistakes:
Starting at tail for forward traversal
Using Prev pointer for forward traversal
Confusing Next and Prev pointers
3. Given the doubly linked list with nodes containing values 1, 2, 3, 4, what is the output of this forward traversal code?
Node current = head;
while(current != null) {
Console.Write(current.Value + " -> ");
current = current.Next;
}
Console.WriteLine("null");
medium
A. null
B. 4 -> 3 -> 2 -> 1 -> null
C. 1 -> 2 -> 3 -> 4 -> null
D. 1 -> 2 -> 3 -> null
Solution
Step 1: Understand the list structure
The list nodes are 1 linked to 2, then 3, then 4, in order.
Step 2: Trace the forward traversal
Starting at head (1), print 1, move to 2, print 2, move to 3, print 3, move to 4, print 4, then current becomes null and loop ends.
Final Answer:
1 -> 2 -> 3 -> 4 -> null -> Option C
Quick Check:
Forward traversal prints nodes in order [OK]
Hint: Follow Next pointers from head to null [OK]
Common Mistakes:
Printing backward order instead of forward
Missing last node in output
Printing null before nodes
4. Identify the error in this backward traversal code of a doubly linked list:
Node current = head;
while(current != null) {
Console.Write(current.Value + " <- ");
current = current.Prev;
}
Console.WriteLine("null");
medium
A. Missing null check in loop
B. Using Prev pointer instead of Next
C. Incorrect print format
D. Starting traversal from head instead of tail
Solution
Step 1: Understand backward traversal
Backward traversal should start from the tail node, not the head.
Step 2: Analyze given code
The code starts at head and tries to move backward using Prev, but head.Prev is null, so loop ends immediately.
Final Answer:
Starting traversal from head instead of tail -> Option D
Quick Check:
Backward traversal must start at tail [OK]
Hint: Backward means start at tail, not head [OK]
Common Mistakes:
Starting backward traversal at head
Confusing Prev and Next pointers
Ignoring list ends (null pointers)
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. Write a method to print the list forward and then backward without using extra data structures. What will be the output?