×

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 :

Related Topics

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.