Dart LinkedListEntry
last modified April 4, 2025
In Dart, LinkedListEntry is a base class for elements in a LinkedList. It provides the linking structure for doubly-linked lists. Each entry knows its next and previous neighbors.
LinkedListEntry must be extended to create custom entry types. The class manages the list structure automatically when entries are added or removed.
Basic LinkedListEntry Usage
Here's a simple example demonstrating how to create and use a custom LinkedListEntry subclass.
import 'dart:collection'; class PersonEntry extends LinkedListEntry<PersonEntry> { final String name; final int age; PersonEntry(this.name, this.age); @override String toString() => '$name ($age)'; } void main() { var list = LinkedList<PersonEntry>(); var alice = PersonEntry('Alice', 30); var bob = PersonEntry('Bob', 25); list.add(alice); list.add(bob); print(list); }
We create a PersonEntry class extending LinkedListEntry. We then create a LinkedList and add two entries. The list maintains the order of insertion.
$ dart main.dart (Alice (30), Bob (25))
Accessing Neighboring Entries
LinkedListEntry provides next and previous properties to traverse the list.
import 'dart:collection'; class TaskEntry extends LinkedListEntry<TaskEntry> { final String description; TaskEntry(this.description); } void main() { var tasks = LinkedList<TaskEntry>(); var task1 = TaskEntry('Buy milk'); var task2 = TaskEntry('Walk dog'); var task3 = TaskEntry('Write code'); tasks.addAll([task1, task2, task3]); print('First task: ${task1.next?.description}'); print('Middle task previous: ${task2.previous?.description}'); print('Middle task next: ${task2.next?.description}'); print('Last task previous: ${task3.previous?.description}'); }
We create a linked list of tasks and demonstrate how to navigate between entries. The next and previous properties return null at list boundaries.
$ dart main.dart First task: Walk dog Middle task previous: Buy milk Middle task next: Write code Last task previous: Walk dog
Removing Entries from List
Entries can be removed from their list by calling unlink() on them.
import 'dart:collection'; class NumberEntry extends LinkedListEntry<NumberEntry> { final int value; NumberEntry(this.value); } void main() { var numbers = LinkedList<NumberEntry>(); var one = NumberEntry(1); var two = NumberEntry(2); var three = NumberEntry(3); numbers.addAll([one, two, three]); print('Before removal: $numbers'); two.unlink(); print('After removal: $numbers'); // Trying to unlink again has no effect two.unlink(); print('After second unlink: $numbers'); }
We demonstrate removing an entry from the middle of the list. The unlink() method can be safely called multiple times on the same entry.
$ dart main.dart Before removal: (1, 2, 3) After removal: (1, 3) After second unlink: (1, 3)
Checking List Membership
LinkedListEntry provides a list property to check if an entry is in a list.
import 'dart:collection'; class CityEntry extends LinkedListEntry<CityEntry> { final String name; CityEntry(this.name); } void main() { var cities = LinkedList<CityEntry>(); var ny = CityEntry('New York'); var la = CityEntry('Los Angeles'); print('Before adding:'); print('NY in list: ${ny.list != null}'); print('LA in list: ${la.list != null}'); cities.add(ny); cities.add(la); print('\nAfter adding:'); print('NY in list: ${ny.list != null}'); print('LA in list: ${la.list != null}'); ny.unlink(); print('\nAfter removing NY:'); print('NY in list: ${ny.list != null}'); print('LA in list: ${la.list != null}'); }
We check list membership using the list property. It returns null when the entry is not in any list, or the LinkedList instance when it is.
$ dart main.dart Before adding: NY in list: false LA in list: false After adding: NY in list: true LA in list: true After removing NY: NY in list: false LA in list: true
Inserting Entries Between Others
Entries can be inserted between existing entries using insertBefore/After.
import 'dart:collection'; class ProductEntry extends LinkedListEntry<ProductEntry> { final String name; final double price; ProductEntry(this.name, this.price); @override String toString() => '$name: \$$price'; } void main() { var products = LinkedList<ProductEntry>(); var apple = ProductEntry('Apple', 0.99); var banana = ProductEntry('Banana', 1.49); products.add(apple); products.add(banana); print('Original list: $products'); var orange = ProductEntry('Orange', 1.29); orange.insertAfter(apple); print('After inserting orange: $products'); var grape = ProductEntry('Grape', 2.99); grape.insertBefore(banana); print('After inserting grape: $products'); }
We demonstrate inserting entries at specific positions in the list. The insert operations automatically update all necessary links in the list.
$ dart main.dart Original list: (Apple: $0.99, Banana: $1.49) After inserting orange: (Apple: $0.99, Orange: $1.29, Banana: $1.49) After inserting grape: (Apple: $0.99, Orange: $1.29, Grape: $2.99, Banana: $1.49)
Best Practices
- Single List Membership: An entry can only belong to one list at a time.
- Proper Removal: Always unlink entries before reusing them.
- Type Safety: Extend LinkedListEntry with a specific type parameter.
- Performance: Insertions and removals are O(1) operations.
Source
Dart LinkedListEntry Documentation
This tutorial covered Dart's LinkedListEntry with practical examples demonstrating its key features and usage patterns.
Author
List all Dart tutorials.