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:
ListQueueis a List based onQueue. Read more aboutListQueuehere.
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 queueListQueue queue = new ListQueue();// add element 10 to end of the queuequeue.addLast(10);print("queue is : $queue");// add element 20 to start of the queuequeue.addFirst(20);print("queue is : $queue");// add element 30 to start of the queuequeue.add(30);print("queue is : $queue");}
Explanation
In the code above:
-
Line 1: We import the
collectionlibrary. -
Line 4: We create a
ListQueuewith the namequeue. -
Line 7: We use the
addlastmethod to add element10as the last element of the queue. Now the queue is{10}. -
Line 11: We use the
addFirsttmethod to add element20as the first element of the queue. Now the queue is{20,10}. -
Line 15: We use the
addmethod to add element30as the last element of the queue. Now the queue is{20,10,30}.