Doubly linked lists
Doubly linked lists also represent a sequence of linked nodes, but each node now stores references to the next and previous nodes.

The doubly linked nature of a list must be taken into account when adding or removing an element, since in addition to a reference to the next element, a reference to the previous element must also be established. However, at the same time, we can traverse the list from the first to the last element, as well as from the last to the first element. Otherwise, a doubly linked list is no different from a singly linked list.
To create a doubly linked list, you first need to define a node class that will represent a list element:
1 2 3 4 5 6 7 8 9 10 | public T Data { get; set; }public DoublyNode<T> Previous { get; set; } public DoublyNode<T> Next { get; set; } } |
Next, we define the list class itself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | using System.Collections.Generic;using System.Collections; namespace SimpleAlgorithmsApp{ public class DoublyLinkedList<T> : IEnumerable<T> // doubly linked list { DoublyNode<T> head; // head/first element DoublyNode<T> tail; // last/tail element int count; // number of elements in the list
// Non-generic enumerator (Fixed to call the generic implementation to avoid stack overflow)
|
Basically, this doubly linked list implements the same actions as a singly linked list; the difference is the need to set the Previous property for the list nodes.
In the add method Add()if the list already contains elements, the property of the node being added Previouspoints to the node that was previously stored in the tail variable:
1 2 3 4 5 6 7 8 | if (head == null) |

Similarly, in the AddFirstprepending method, the head element’s Previous property begins pointing to the new element, and the new element thus becomes the first element in the list.

When deleting, you first need to find the element to be deleted. Then, in general, you need to reset two links:
1 2 | current.Next.Previous = current.Previous; |
If the first and last elements are removed, the head and tail variables must be reset accordingly.

And also, unlike the single-linked implementation, a method BackEnumerator()for iterating over elements from the end has been added here.
Using the list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Forward iteration:
Kate
Bob
Bill
Tom
Backward iteration after removing Bill:
Tom
Bob
Kate
Explore More IT Terms
A
B
C
D
E
F
H
K
W
- What are databases, and why do they need DBMS and SQL?
- What do Linux distributions consist of?
- What is a GPU in a computer, in simple terms?
- What is Linux? The History of Linux
- What is the OSI Model: A Complete Explanation of the Seven Layers and Their Role in Networking
- Which Linux distribution should you choose? A Linux distribution overview
