Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
doublelinkedqueue
add
data structures
communitycreator

How to add an element to a DoubleLinkedQueue in Dart

Surabhi Sharma

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}

RELATED TAGS

dart
doublelinkedqueue
add
data structures
communitycreator
RELATED COURSES

View all Courses

Keep Exploring