📖 Scenario: Imagine you are building a simple music playlist app. Each song in the playlist can move forward or backward easily. To do this, you will create a doubly linked list where each song is a node connected to the previous and next songs.
🎯 Goal: You will build the basic structure of a doubly linked list by creating a Node class and a DoublyLinkedList class with methods to add songs. This will help you understand how nodes connect in both directions.
📋 What You'll Learn
Create a Node class with data, prev, and next properties
Create a DoublyLinkedList class with head and tail properties
Add a method append to add new nodes to the end of the list
Print the list from head to tail showing song names separated by ' <-> '
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, web browsers (back and forward navigation), and undo-redo features where moving in both directions is needed.
💼 Career
Understanding doubly linked lists helps in software development roles that require efficient data manipulation and navigation, such as building user interfaces, games, or complex data structures.
Progress0 / 4 steps
1
Create the Node class
Create a Node class with a constructor that takes a data string parameter. Add public properties data (string), prev (Node or null), and next (Node or null). Initialize prev and next to null.
DSA Typescript
Hint
Think of each Node as a song in the playlist. It needs to know its name (data) and links to the previous and next songs (prev and next).
2
Create the DoublyLinkedList class with head and tail
Create a DoublyLinkedList class with public properties head and tail, both initialized to null. These will track the first and last nodes in the list.
DSA Typescript
Hint
The DoublyLinkedList keeps track of the first song (head) and the last song (tail) in the playlist.
3
Add an append method to add nodes to the list
Inside the DoublyLinkedList class, add a public method append that takes a data string. Create a new Node with this data. If the list is empty (head is null), set both head and tail to the new node. Otherwise, link the new node after tail and update tail to the new node.
DSA Typescript
Hint
When adding a new song, if the playlist is empty, the new song is both the first and last. Otherwise, connect it after the last song and update the tail.
4
Print the list from head to tail
Add code to create a DoublyLinkedList instance called playlist. Append these songs in order: 'Song A', 'Song B', 'Song C'. Then, print the playlist from head to tail by traversing nodes and joining their data with ' <-> '. Use console.log to display the result.
DSA Typescript
Hint
Start from the head and move to the next node until you reach the end (null). Collect each song's name and join them with ' <-> '.
Practice
(1/5)
1. What is the main difference between a doubly linked list node and a singly linked list node?
easy
A. A doubly linked list node has pointers to both the previous and next nodes.
B. A doubly linked list node stores data in a different format.
C. A doubly linked list node can only point to the next node.
D. A doubly linked list node does not store any data.
Solution
Step 1: Understand node pointers in singly linked list
A singly linked list node has only one pointer to the next node.
Step 2: Understand node pointers in doubly linked list
A doubly linked list node has two pointers: one to the previous node and one to the next node.
Final Answer:
A doubly linked list node has pointers to both the previous and next nodes. -> Option A
Quick Check:
Doubly linked list nodes have two pointers [OK]
Hint: Remember: doubly linked means two directions [OK]
Common Mistakes:
Thinking doubly linked nodes only point forward
Confusing data storage with pointer structure
Assuming doubly linked nodes do not store data
2. Which of the following is the correct TypeScript syntax to define a doubly linked list node class with data of type number?
C. Change next and prev types to 'Node | null' to allow null assignment.
D. Initialize next and prev with new Node() instead of null.
Solution
Step 1: Analyze type declarations and assignments
next and prev are typed as Node but assigned null, which is invalid in TypeScript.
Step 2: Fix type to allow null
Changing next and prev types to 'Node | null' allows null assignment and fixes the error.
Final Answer:
Change next and prev types to 'Node | null' to allow null assignment. -> Option C
Quick Check:
Nullable pointers need 'Node | null' type [OK]
Hint: Allow null in pointer types to avoid errors [OK]
Common Mistakes:
Leaving next and prev as non-nullable types
Trying to assign new Node() without data
Removing constructor unnecessarily
5. You want to insert a new node with data 30 between two existing nodes in a doubly linked list: nodeA and nodeB, where nodeA.next = nodeB and nodeB.prev = nodeA. Which sequence of pointer updates correctly inserts the new node?