Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What is the DoubleLinkedQueue.addLast() method in Dart?

Surabhi Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

The addLast() 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 that is added first will be deleted first.

The figure below illustrates how the addLast() method works.

Adding an element to the end of the queue using the addLast() method

Syntax

void addLast(E value)

Parameters

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

Return value

The addLast() method does not return anything and is a constant-time operation.

Code

The code below shows how the addLast() method works in Dart.

import 'dart:collection'; 
void main() { 
   var monthQueue = DoubleLinkedQueue<String>();
   
   monthQueue.add("January"); 
   monthQueue.add("February"); 
   monthQueue.add("March");   
   
   print('Printing Queue Elements');
   print(monthQueue); 

   monthQueue.addLast("April");
   monthQueue.addLast("May");

   print('Printing Queue Elements');
   print(monthQueue);

   
}  

Explanation

  • We create an instance of a DoubleLinkedQueue class of type String.

  • Next, we use the add method to add a few strings ("January", "February", and "March") to the queue.

Notice that all of the elements are added to the end of the queue, as the queue is a FIFO data structure.

  • We now use the addLast() method to add "April" and "May" to the queue.

  • We use the print() function of the core library to display the queue elements. We can observe that "April" and "May" are added at the end of the queue.

RELATED TAGS

dart
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring