ZetCode

Dart ListQueue

last modified April 4, 2025

In Dart, ListQueue is an efficient list-based implementation of the Queue interface. It provides O(1) operations for adding/removing at both ends.

ListQueue combines benefits of both lists and queues. It supports index-based access like lists while maintaining efficient queue operations.

Creating a ListQueue

We can create a ListQueue using its constructor or from an existing iterable.

main.dart
import 'dart:collection';

void main() {
  var queue = ListQueue<String>();
  queue.add('apple');
  queue.add('banana');
  queue.add('orange');

  print(queue);
}

This creates an empty ListQueue and adds three elements. The generic type parameter specifies the element type as String.

$ dart main.dart
{apple, banana, orange}

Adding Elements

ListQueue provides multiple methods for adding elements at different positions.

main.dart
import 'dart:collection';

void main() {
  var queue = ListQueue.of([1, 2, 3]);
  
  // Add to end
  queue.add(4);
  
  // Add to start
  queue.addFirst(0);
  
  // Add multiple
  queue.addAll([5, 6]);
  
  print(queue);
}

We demonstrate adding elements to both ends of the queue. addFirst is particularly useful for queue operations.

$ dart main.dart
{0, 1, 2, 3, 4, 5, 6}

Removing Elements

ListQueue provides several ways to remove elements from the collection.

main.dart
import 'dart:collection';

void main() {
  var queue = ListQueue.of(['a', 'b', 'c', 'd', 'e']);
  
  // Remove first
  var first = queue.removeFirst();
  
  // Remove last
  var last = queue.removeLast();
  
  // Remove specific element
  queue.remove('c');
  
  print('Removed: $first, $last');
  print('Remaining: $queue');
}

This shows FIFO (removeFirst) and LIFO (removeLast) operations. The remove method deletes the first occurrence of the specified value.

$ dart main.dart
Removed: a, e
Remaining: {b, d}

Accessing Elements

ListQueue supports both queue-style access and index-based element retrieval.

main.dart
import 'dart:collection';

void main() {
  var queue = ListQueue.of([10, 20, 30, 40, 50]);
  
  // Peek first/last
  print('First: ${queue.first}');
  print('Last: ${queue.last}');
  
  // Index access
  print('Element at 2: ${queue.elementAt(2)}');
  
  // Iteration
  print('All elements:');
  for (var item in queue) {
    print(item);
  }
}

ListQueue maintains list-like characteristics while providing queue operations. We can access elements by position or iterate through them.

$ dart main.dart
First: 10
Last: 50
Element at 2: 30
All elements:
10
20
30
40
50

Queue Operations

ListQueue implements standard queue operations with efficient performance.

main.dart
import 'dart:collection';

void main() {
  var queue = ListQueue<int>();
  
  // Enqueue elements
  queue.addLast(1);
  queue.addLast(2);
  queue.addLast(3);
  
  // Dequeue elements
  while (queue.isNotEmpty) {
    print('Processing: ${queue.removeFirst()}');
  }
  
  print('Queue empty: ${queue.isEmpty}');
}

This demonstrates classic FIFO queue behavior. Elements are added at the end and removed from the front. The isNotEmpty check is safer than checking length.

$ dart main.dart
Processing: 1
Processing: 2
Processing: 3
Queue empty: true

Best Practices

Source

Dart ListQueue Documentation

This tutorial covered Dart's ListQueue with practical examples demonstrating its key features and usage patterns for efficient queue operations.

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.