Table of contents
1.
Introduction
2.
Queues In Dart
2.1.
Creating Queue using Constructor
2.2.
Creating Queue Using Existing List
3.
Methods of Queue in Dart
4.
Example
5.
Frequently Asked Questions
5.1.
When to use a stack, queue, and list?
5.2.
Is it true that queues are faster than stacks?
5.3.
What is the difference between a static and a dynamic queue?
6.
Conclusion
Last Updated: Mar 27, 2024

Dart Queues

Author Akshit Pant
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Queue is a collection that can easily be manipulated from both ends. ForEach or an Iterator can be used to iterate over the elements of a queue.

In this article, we will be learning about Dart Queues, their initialization, methods & properties with an example.

Before learning how we do so, learning the basic Introduction of Dart is recommended.

So, let us start reading about Queues in Dart.

Queues In Dart

Dart also allows users to manipulate data in the form of a queue. A queue is a First In, First Out (FIFO) Data Structure in which the first thing added is discarded first. It takes the data from the first end and removes it from the second end. So, When you want to create a first-in, first-out data collection, queues come in handy. Now let us see the ways of creating a queue in Dart.

Creating Queue using Constructor

Let us see the syntax for creating a Queue in Dart and then inserting the elements in it.

Syntax:

Queue var_name = new Queue();

Input:

import 'dart:collection';
 
void main()
{
  // Creating Queue with var_name codeStudio
  Queue<String> codeStudio = new Queue<String>();
   
  // Adding elements
  codeStudio.add("Code");
  codeStudio.add("Studio");
  codeStudio.add("Blog");
   
  // Printing all the inserted elements
  print(codeStudio);
}

Output:

{Code, Studio, Blog}

Creating Queue Using Existing List

Now, let us see the syntax for creating a Queue in Dart through Existing List and then inserting the elements in it.

Syntax:

Queue<E> var_name = new Queue<E>.from(list_name); // With type notation(E)

var var_name = new Queue.from(list_name); // Without type notation

Input:

import 'dart:collection';
 
void main()
{
  // Creating List
  List<String> my_list = ["Code","Studio","Blog"];
   
  // Creating a Queue (my_queue) through a List (my_list)
  Queue<String> my_queue = new Queue<String>.from(my_list);
   
  // Printing elements of the queue my_queue
  print(my_queue);
}

Output:

{Code, Studio, Blog}

Methods of Queue in Dart

Dart also has Methods for manipulating queues that have been built. In the table below, some significant functions are listed, followed by an example of how to utilize them.

  • queue_name.add(element): From the front, adds the element to the queue.
  • queue_name.addAll(collection_name): All the elements in the collection_name are added (generally List).
  • queue_name.addFirst(element): The element from the front is added to the queue.
  • queue_name.addLast(element): The element is added from the back of the queue.
  • queue_name.clear(): All elements in the queue are removed.
  • queue_name.first(): The first entry from the queue is returned.
  • queue_name.forEach(f(element)): This method returns all of the elements in the queue.
  • queue_name.isEmpty: If the queue is empty, it returns true; otherwise, it returns false.
  • queue_name.length: The length of the queue is returned.
  • queue_name.removeFirst(): The first entry in the queue is removed.
  • queue_name.removeLast(): The last entry in the queue is removed.

Example

This example given below shows the use of various functions on a Queue in Dart.

Code:

import 'dart:collection';
 
void main()
{
  Queue<String> myVar = new Queue<String>(); //  Queue Creation
   
  // Adding and displaying element
  myVar.add("Coding");
  print(myVar);
   
  // Adding multiple elements and displaying them
  List<String> my_data_list = ["Ninja","Blogs"];
  myVar.addAll(my_data_list);
  print(myVar);
   
  // Deleting all the data elements from queue
  myVar.clear();
  print(myVar);
   
  // Ckecking if queue is empty or not: empty queue returns true, otherwise false
  print(myVar.isEmpty);
   
  // Adding first element
  myVar.addFirst("Code");
  print(myVar);
   
  //Adding Last element
  myVar.addLast("Studio");
  myVar.addLast("Presents");
  print(myVar);
     
  // check length of queue
  print(myVar.length);
   
  // Remove the First Element from the queue
  myVar.removeFirst();
  print(myVar);
   
  // Remove the Last element from the queue
  myVar.removeLast();
  print(myVar);
   
  // Display all the elements currently in the queue
  myVar.forEach(print);
}

Output:

{Coding}
{Coding, Ninja, Blogs}
{}
true
{Code}
{Code, Studio, Presents}
3
{Studio, Presents}
{Studio}
Studio

Note: To use a queue in a dart program, you must first import the 'Dart:collection' module. If you do not, you will encounter the compilation error.

Read about Application of Queue in Data Structure here.

Frequently Asked Questions

When to use a stack, queue, and list?

Use a queue when you want to get items out in the same order that you put them in. When you need to get things out in the opposite order that they were put in, use a stack. Use a list when you need to get anything out, regardless of when you put it in (and when you don't want it to be automatically removed).

Is it true that queues are faster than stacks?

While the speed of queue and stack isn't drastically different, they do cause a distinct order of node visits. Depending on how your nodes are set up in memory, one may give a more cache-friendly order than the other.

What is the difference between a static and a dynamic queue?

A static queue has been defined ahead of time and whose definition is stored in the environment. A dynamic queue, on the other hand, is built on demand.

Conclusion

In this article, we learned about Dart Queues, their creation, and their methods with a working example.

You can head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications.

Check out this problem - Queue Implementation

We hope this article has helped you enhance your knowledge of Dart programming and Input-Output in it. If you want to learn more, check out our Programming Fundamentals and Competitive Programming articles. Do upvote this article to help other ninjas grow.

Live masterclass