Dart Queues
A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first, will be deleted first.
A queue has two ends = front and rear. Elements are inserted at one end and deleted from the other end.
Note : Don’t forget to import ‘ dart : collection ’ module in the Dart program containing queue.
In Dart, queue can be created using following two methods :
- Using queue( ) constructor
- Using existing list
Creating a queue using constructor
Dart provides an in-built constructor Queue( ) to create the data structure queue.
Implementation = Queue queue_name = new Queue( ) ;
Program
import 'dart:collection' ;
void main( )
{
// declaring queue using queue( ) constructor
Queue< String > que = new Queue< String >( ) ;
// printing the elements of the queue.
// Nothing will be printed as queue is empty as of now
print( que ) ;
// initializing queue with the string elements
que.add( " This " ) ;
que.add( " is " ) ;
que.add( " a " ) ;
que.add( " Queue " ) ;
// printing the elements of the queue
print( que ) ;
}
Output :
{ }
{ This, is, a, Queue }
Creating a queue using existing list
Program
import 'dart:collection' ;
void main( )
{
// creating a list with string elements
List< String > queue_list = [ " This ", " creates ", " queue ", "
using ", " existing ", " list " ] ;
// Creating a Queue using an existing list
Queue< String > que = new Queue< String >.from( queue_list ) ;
// Printing the elements of the queue
print( que ) ;
}
Output :
{ This, creates, queue, using, existing, list }
Functions of Queue in Dart
There are some functions provided by Dart to manipulate queues. Some of the functions are :
Sr.No | Function Syntax | Description of the Function |
1. | queue_name.add ( element ) | This function adds the element to the queue from the front end. |
2. | queue_name.addAll( collection_name ) | This function adds all the elements passed in the place of collection_name (generally List) as an argument. |
3. | queue_name.addFirst( element ) | This function adds the element in the queue at the beginning from the front. |
4. | queue_name.addLast( element ) | This function adds the element in the queue at the end from the rear end. |
5. | queue_name.clear( ) | This function clears the queue by deleting all the elements from the queue. |
6. | queue_name.first( ) | This function returns the first element of the queue from the beginning. |
7. | queue_name.forEach( f ( element ) ) | This function returns all the elements present in the queue. |
8. | queue_name.isEmpty | This function checks if the queue is empty or not and accordingly returns true or false. |
9. | queue_name.length | This function calculates the length of the queue and returns the same. |
10. | queue_name.removeFirst( ) | This function deletes the first element from the queue. |
11. | queue_name.removeLast( ) | This function deletes the last element from the queue. |
Program
import 'dart:collection' ;
void main( )
{
// Creating a Queue
Queue<String> str = new Queue<String>( ) ;
//printing the values of queue
print( " \n Values of the Queue : " ) ;
print( str ) ;
// checking length of the queue str
print( " \n Length of the Queue : " ) ;
print( str.length ) ;
// adding a single element to the queue
str.add( " \n Hello " ) ;
// printing the values of the queue
print( " \n Values of the Queue : " ) ;
print( str ) ;
// checking length of the queue str
print( " \n Length of the Queue : " ) ;
print( str.length ) ;
// deleting the whole queue str
str.clear( ) ;
print( " \n Values of the Queue : " ) ;
print( str ) ;
// check if the queue str is empty or not
print( " \n Queue is Empty ? " ) ;
print( str.isEmpty ) ;
// adding first element in the beginning of the queue str
str.addFirst( " Java " ) ;
print( " \n Values of the Queue : " ) ;
print( str ) ;
//Adding the last element in the queue str
str.addLast( " Tpoint " ) ;
print( " \n Values of the Queue : " ) ;
print( str ) ;
// removing the first element of the queue str
str.removeFirst( ) ;
print( " \n Values of the Queue : " ) ;
print( str ) ;
// remving the last element of the queue str
str.removeLast( ) ;
print( " \n Values of the Queue : " ) ;
print( str ) ;
// printing the whole queue str
print( " \n Values of the Queue : " ) ;
str.forEach( print ) ;
}
Output :
Values of the Queue :
{}
Length of the Queue :
0
Values of the Queue :
{
Hello }
Length of the Queue :
1
Values of the Queue :
{}
Queue is Empty ?
true
Values of the Queue :
{ Java }
Values of the Queue :
{ Java , Tpoint }
Values of the Queue :
{ Tpoint }
Values of the Queue :
{}
Values of the Queue :