double_linked_list 1.0.8

Dart native js
Flutter Android iOS web

A Generic Double Linked List data structure. Allows constant time inserting elements at both ends, before and after any element and constant time element deletion.

Double Linked List #

A library presenting a generic double linked list.

Double linked list structure implemented:

dll_structure

Usage #

Create a new empty list, from iterables or from another list

import 'package:double_linked_list/double_linked_list.dart';

main() {
  final emptyList = DoubleLinkedList<int>.empty();
  
  final fromList = DoubleLinkedList<int>.fromIterable([1, 2, 3]);
  final fromListExtension = [1, 2, 3].toDoubleLinkedList();
  
  final fromSet = DoubleLinkedList<int>.fromIterable({1, 2, 3});
  final fromSetExtension = {1, 2, 3}.toDoubleLinkedList();
  
  final copyList = DoubleLinkedList<int>.from(fromList);
  final copyListMethod = copyList.copy();
}

Iterate over a list in normal or reverse order

import 'package:double_linked_list/double_linked_list.dart';

main() {
  final list = [1, 3, 5].toDoubleLinkedList();
  for (var node = list.first; !node.isEnd; node = node.next) {
    node = node.insertAfter(node.content + 1);
  }
  print(list); // [1, 2, 3, 4, 5, 6]
  
  for (var node = list.last; !node.isBegin; node = node.previous) {
    print(node.content);
  }
  // 6 5 4 3 2 1
}

Be careful when iterating and inserting elements:

import 'package:double_linked_list/double_linked_list.dart';

main() {
  final list = [1, 3, 5].toDoubleLinkedList();
  for (var node = list.first; !node.isEnd; node = node.next) {
    node.insertAfter(node.content + 1);
  }
}

⚠️Program stuck at an endless loop.

To avoid this, move to the inserted node:

import 'package:double_linked_list/double_linked_list.dart';

main() {
  final list = [1, 3, 5].toDoubleLinkedList();
  for (var node = list.first; !node.isEnd; node = node.next) {
    node = node.insertAfter(node.content + 1);
  }
}

You can also use most of the functions available for Iterables, and some others:

import 'package:double_linked_list/double_linked_list.dart';

main() {
  final list = [1, 2, 3].toDoubleLinkedList();

  list.forEach(print);

  list.apply((e) => e * e);
  print(list); // [1 | 4 | 9]
    
  print(list.where((e) => e.isOdd)); // [1 | 9]
    
  print(list.reduce((value, element) => value + element)); // 14
  print(list.fold<int>(-14, (value, element) => value + element)); // 0
    
  print(list.any((e) => e > 2)); // true
  print(list.every((e) => e > 2)); // false
    
  print(list.firstWhere((e) => e > 2)); // (4)
  print(list.lastWhere((e) => e > 2)); // (9)
  
}

Complete example at example.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

2
likes
110
pub points
17%
popularity

A Generic Double Linked List data structure. Allows constant time inserting elements at both ends, before and after any element and constant time element deletion.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

vdiviloper@gmail.com

License

BSD (LICENSE)

Dependencies

equatable

More

Packages that depend on double_linked_list