Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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 :

  1. Using queue( ) constructor
  2. 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.NoFunction SyntaxDescription 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.isEmptyThis function checks if the queue is empty or not and accordingly returns true or false.
9.queue_name.lengthThis 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 :