ZetCode

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.

main.dart
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.

main.dart
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.

main.dart
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.

main.dart
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.

main.dart
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

Source

Dart LinkedListEntry Documentation

This tutorial covered Dart's LinkedListEntry with practical examples demonstrating its key features and usage patterns.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.