How to add an element to a DoubleLinkedQueue in Dart

The dart:collection library provides the advanced collection support for the Dart language, apart from the basic collections already provided by the dart:core library.

It contains the DoubleLinkedQueue<E> class which implements the abstract Queue<E> class, using a doubly-linked list.

The add() method

The DoubleLinkedQueue<E> class contains the add() method, which adds an element to the end of a queue.

A queue is a FIFO (First In First Out) data structure. In a queue, the element which is added first will be deleted first.

Syntax

void add(
E value
)

Arguments

  • This method takes a value of type E as input and adds it to the end of the queue.

Return value

  • This method does not return anything
  • This is a constant-time operation

Code

We first import the dart:collection library into our program.

import 'dart:collection';
void main() {
var dayQueue = DoubleLinkedQueue<String>();
dayQueue.add("Sunday");
dayQueue.add("Monday");
dayQueue.add("Tuesday");
print('Printing Queue Elements');
print(dayQueue);
}

Explanation

  • We have created an instance of a DoubleLinkedQueue class of type String.
  • Next, we add a few strings to the queue: "Sunday", "Monday", and "Tuesday", using the add() method.
  • Notice that all elements are added to the end of the queue, as the queue is a FIFO data structure.
  • The queue elements are displayed using the print() function of the core library.

Output

The program prints the output below and exits:

Printing Queue Elements
{Sunday, Monday, Tuesday}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources