The dart:collection library provides advanced collection support for the Dart language. The library contains the DoubleLinkedQueue<E> class, which uses a doubly-linked list to implement the abstract Queue<E> class.
addFirst() methodThe DoubleLinkedQueue<E> class contains the addFirst() method which adds an element to the start of a queue.
A queue is a FIFO (First In First Out) data structure. In a queue, the element that is added first will be deleted first.
The figure below illustrates how the addFirst() method works:
The syntax of the addFirst() method is as follows:
void addFirst(E value)
addFirst method takes a value of type E as input and adds it to the beginning of the queue.addFirst does not return anything and it is a constant-time operation.
The code below shows how the addFirst() method works in Dart:
import 'dart:collection';void main() {var monthQueue = DoubleLinkedQueue<String>();monthQueue.add("February");monthQueue.add("March");monthQueue.add("April");print('Printing Queue Elements');print(monthQueue);monthQueue.addFirst("January");print('Printing Queue Elements');print(monthQueue);}
DoubleLinkedQueue class of type String.add() method to add a few strings to the queue: "February", "March", and "April"."January" to the beginning of the queue with the addFirst() method.print() function of the code library to display the queue elements, and it can be observed that the queue contains "January" at the start.Free Resources