How to add an element at the start and end of ListQueue in Dart

Overview

The addLast method is used to add an element at the end of the ListQueue.

We can use the add or addFirst methods to add an element at the start of the ListQueue.

Note:ListQueue is a List based on Queue. Read more about ListQueue here.

Synax

// add element at end
void addLast(E value)

// add element at start

void add(E value)
void addFirst(E value)

Parameter

The element to be added to the queue is passed as a parameter.

Return value

This method doesn’t return any value.

Code

The code below demonstrates how to add an element to the start and end of the ListQueue:

import 'dart:collection';
void main() {
// create a queue
ListQueue queue = new ListQueue();
// add element 10 to end of the queue
queue.addLast(10);
print("queue is : $queue");
// add element 20 to start of the queue
queue.addFirst(20);
print("queue is : $queue");
// add element 30 to start of the queue
queue.add(30);
print("queue is : $queue");
}

Explanation

In the code above:

  • Line 1: We import the collection library.

  • Line 4: We create a ListQueue with the name queue.

  • Line 7: We use the addlast method to add element 10 as the last element of the queue. Now the queue is {10}.

  • Line 11: We use the addFirstt method to add element 20 as the first element of the queue. Now the queue is {20,10}.

  • Line 15: We use the add method to add element 30 as the last element of the queue. Now the queue is {20,10,30}.

Free Resources