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
// 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.

Similar Reads

Dart Collection

There are 5 Interfaces that we have in Dart Collection as mentioned below:...

1. List Interface

The list is an ordered group of objects where each object is from one specific type. To define a list in dart, specify the object type inside the angled brackets (<>)....

2. Set Interface

Sets are one of the essential part of Dart Collections. A set is defined as an unordered collection of unique objects....

3. Map Interface

In Dart, Maps are unordered key-value pair collection that sets an associate key to the values within. To define a Map, specify the key type and the value type inside the angle brackets(<>) as shown below:...

4. Queue Interface

Queues are used to implement FIFO(First in First Out) collection. This collection can be manipulated from both ends....

5. 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....

Contact Us