📜  Dart的队列

📅  最后修改于: 2021-09-02 05:20:54             🧑  作者: Mango

Dart还提供给用户以队列的形式操作一组数据。 A Q ueueFIFO(先入先出)的数据结构,其中首先被添加的元素将被首先删除。它从一端获取数据并从另一端删除它当您想要构建先进先出的数据集合时,队列很有用。它是Dart中数据列表实现的特例。

在Dart创建队列:

使用构造函数:

Queue variable_name = new Queue();

通过现有列表:

// With type notation(E)
Queue variable_name = new Queue.from(list_name);

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

必须注意,要在dart程序中使用队列,您必须导入 dart:collection 模块。如果不这样做,则会看到以下错误

Error compiling to JavaScript:
main.dart:6:3:
Error: 'Queue; isn't a type
  Queue geek = new Queue();
  ^^^^^
main.dart:6:28:
Error: Method not found: 'Queue'.
  Queue geek = new Queue();
                           ^^^^^
Error: Compilation failed.

示例 1:通过构造函数创建队列,然后在其中插入元素。

Dart
import 'dart:collection';
  
void main()
{
  // Creating a Queue
  Queue geek = new Queue(); 
    
  // Printing default
  // value of queue
  print(geek);
    
  // Adding elements in a Queue
  geek.add("Geeks");
  geek.add("For");
  geek.add("Geeks");
    
  // Printing the
  // inserted elements
  print(geek);
}


Dart
import 'dart:collection';
  
void main()
{
  // Creating a List
  List geek_list = ["Geeks","For","Geeks"];
    
  // Creating a Queue through a List
  Queue geek_queue = new Queue.from(geek_list); 
    
  // Printing the elements
  // in the queue
  print(geek_queue);
}


Dart
import 'dart:collection';
  
void main()
{
  // Creating a Queue
  Queue geek = new Queue(); 
    
  // Printing default
  // value of queue
  print(geek);
    
  // Adding a element
  // and displaying it
  geek.add("Geeks");
  print(geek);
    
  // Adding multipl
  // element and displaying it
  List geek_data = ["For","Geeks"];
  geek.addAll(geek_data);
  print(geek);
    
  // Deleting all the
  // data from queue
  geek.clear();
  print(geek);
    
  // Checking if the
  // queue is empty or not
  print(geek.isEmpty);
    
  // Adding first element
  geek.addFirst("Geeks");
  print(geek);
    
  //Adding Last element
  geek.addLast("For");
  geek.addLast("Geeks");
  print(geek);
      
  // Checking length of the queue
  print(geek.length);
    
  // Removing First
  // Element from queue
  geek.removeFirst();
  print(geek);
    
  // Removing Last
  // element from queue 
  geek.removeLast();
  print(geek);
    
  // Displaying all the
  // elements of the queue
  geek.forEach(print);
}


输出:

{}
{Geeks, For, Geeks}

上面代码中queue_name.add(element)用于添加队列中的数据。

示例 2:通过列表创建队列。

Dart

import 'dart:collection';
  
void main()
{
  // Creating a List
  List geek_list = ["Geeks","For","Geeks"];
    
  // Creating a Queue through a List
  Queue geek_queue = new Queue.from(geek_list); 
    
  // Printing the elements
  // in the queue
  print(geek_queue);
}

输出:

{Geeks, For, Geeks}

Dart中队列的功能:

Dart还提供了操作在dart创建的队列的函数。下表列出了一些重要的功能,然后是使用它们的示例。

Sr.No.

Function Syntax

Description of the Function

1.

queue_name.add(element) Adds the element inside the queue from the front.

2.

queue_name.addAll(collection_name)

Adds all the element present in the collection_name (generally List).

3.

queue_name.addFirst(element) Adds the element from front inside the queue.

4.

queue_name.addLast(element) Adds the element from back in the queue.

5.

queue_name.clear() Removes all the elements from the queue.

6.

queue_name.first() Returns the first element from the queue.

7.

queue_name.forEach(f(element)) Returns all the element present in the queue.

8.

queue_name.isEmpty Returns boolean true if the queue is empty else return false.

9.

queue_name.length Returns the length of the queue.

10.

queue_name.removeFirst() Removes the first element from the queue.

11.

queue_name.removeLast() Removes the last element from the queue.

示例:在Dart对 Queue 使用各种函数。

Dart

import 'dart:collection';
  
void main()
{
  // Creating a Queue
  Queue geek = new Queue(); 
    
  // Printing default
  // value of queue
  print(geek);
    
  // Adding a element
  // and displaying it
  geek.add("Geeks");
  print(geek);
    
  // Adding multipl
  // element and displaying it
  List geek_data = ["For","Geeks"];
  geek.addAll(geek_data);
  print(geek);
    
  // Deleting all the
  // data from queue
  geek.clear();
  print(geek);
    
  // Checking if the
  // queue is empty or not
  print(geek.isEmpty);
    
  // Adding first element
  geek.addFirst("Geeks");
  print(geek);
    
  //Adding Last element
  geek.addLast("For");
  geek.addLast("Geeks");
  print(geek);
      
  // Checking length of the queue
  print(geek.length);
    
  // Removing First
  // Element from queue
  geek.removeFirst();
  print(geek);
    
  // Removing Last
  // element from queue 
  geek.removeLast();
  print(geek);
    
  // Displaying all the
  // elements of the queue
  geek.forEach(print);
}

输出:

{}
{Geeks}
{Geeks, For, Geeks}
{}
true
{Geeks}
{Geeks, For, Geeks}
3
{For, Geeks}
{For}
For