📖 Scenario: You are building a simple doubly linked list to store numbers. This list allows you to move forward and backward through the numbers easily, like flipping pages in a book.
🎯 Goal: Create the basic structure of a doubly linked list by defining a Node class and a DoublyLinkedList class with a head pointer. This setup will prepare you to add and remove items later.
📋 What You'll Learn
Create a Node class with int data, Node next, and Node prev fields.
Create a DoublyLinkedList class with a Node head field initialized to null.
Use public access modifiers for the classes and fields.
No methods are required yet; just the structure.
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players to move forward and backward through songs, or in web browsers to navigate back and forth between pages.
💼 Career
Understanding how to build and use doubly linked lists is important for software developers working with data structures that require efficient two-way traversal.
Progress0 / 4 steps
1
Create the Node class
Create a public class called Node with three public fields: an int data, a Node next, and a Node prev.
DSA C#
Hint
Think of Node as a box that holds a number and links to the boxes before and after it.
2
Create the DoublyLinkedList class with head
Create a public class called DoublyLinkedList with a public field Node head initialized to null.
DSA C#
Hint
The head points to the first node in the list or null if the list is empty.
3
Add a constructor to Node to set data
Add a public constructor to the Node class that takes an int data parameter and sets the data field. Initialize next and prev to null inside the constructor.
DSA C#
Hint
The constructor helps create a new node with a number and no links yet.
4
Print the head node's data or Empty
Write a Main method that creates a DoublyLinkedList object called list. Then print list.head.data if head is not null, otherwise print "Empty".
DSA C#
Hint
Since the list is empty, head is null, so print "Empty".
Practice
(1/5)
1. What is the main advantage of a Doubly Linked List compared to a singly linked list?
easy
A. It allows traversal in both forward and backward directions.
B. It uses less memory than a singly linked list.
C. It does not require a head pointer.
D. It automatically sorts the elements.
Solution
Step 1: Understand the structure difference
A doubly linked list has nodes with two pointers: one to the next node and one to the previous node.
Step 2: Identify traversal capabilities
This allows moving forward and backward through the list, unlike singly linked lists which only move forward.
Final Answer:
It allows traversal in both forward and backward directions. -> Option A
Quick Check:
Doubly linked list = two-way traversal [OK]
Hint: Remember: two pointers mean two directions [OK]
Common Mistakes:
Thinking it uses less memory than singly linked list
Assuming it doesn't need a head pointer
Believing it sorts elements automatically
2. Which of the following is the correct way to declare a node class for a doubly linked list in C#?
easy
A. class Node { public int data; public Node next; }
B. public class Node { public int data; public Node next; public Node prev; }
C. public class Node { int data; Node next; }
D. public struct Node { int data; Node next; Node prev; }
Solution
Step 1: Check for public access and all pointers
The node must have public fields for data, next, and prev to represent doubly linked list nodes.
Step 2: Verify syntax correctness
public class Node { public int data; public Node next; public Node prev; } declares all required fields with public access, suitable for a doubly linked list node.
Final Answer:
public class Node { public int data; public Node next; public Node prev; } -> Option B
Quick Check:
Node class with data, next, prev = public class Node { public int data; public Node next; public Node prev; } [OK]
Hint: Doubly linked node needs next and prev pointers public [OK]
Common Mistakes:
Omitting the prev pointer
Using struct instead of class (less common for linked lists)
Not making fields public for access
3. What will be the output of the following C# code snippet for a doubly linked list node?