LinkedList Interface
It is a specialized double-linked list of elements. This allows const time adding and removing at the either end also the time to increase the size is constant.
Syntax:
LinkedList<E extends LinkedListEntry<E>>
Example:
Below is the implementation of Doubly Linked List in Dart:
// Dart Program to Implement
// Doubly Linked List
import 'dart:collection';
// Class that extends LinkedListEntry.
// Each item in the LinkedList will be
// an instance of this class.
base class Box extends LinkedListEntry<Box> {
final String contents;
final int number;
Box(this.contents,this.number);
}
void main() {
final myLinkedList = LinkedList<Box>();
// Adding elements to the LinkedList
myLinkedList.add(Box('First Box',1));
myLinkedList.add(Box('Second Box',2));
myLinkedList.add(Box('Third Box',3));
// Iterating over the LinkedList
for (final box in myLinkedList) {
print(" ${box.contents} , ${box.number}");
}
// Remove an element from the LinkedList
final boxToRemove = myLinkedList.firstWhere((box) => box.contents == 'Second Box');
boxToRemove.unlink();
print('');
print('After removal:');
for (final box in myLinkedList) {
print(" ${box.contents} , ${box.number}");
}
}
Output:
First Box , 1
Second Box , 2
Third Box , 3
After removal:
First Box , 1
Third Box , 3
Classes Associated with LinkedList Interface
Class | Description |
---|---|
LinkedListEntry<E extends LinkedListEntry<E>> | An element of a LinkedList. |
Dart – Collections
Collections are groups of objects that represent a particular element. The dart::collection library is used to implement the Collection in Dart. There are a variety of collections available in Dart.