×

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 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 Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 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.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 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 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.

Dart Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute 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.