Queue queue_name = new Queue();
//Queue from an existing List
var queue_name = new Queue.from(list_name);
import 'dart:collection';
void main()
{
Queue<String> Example_queue=new Queue<String>();
print(Example_queue);
Example_queue.add('This');
Example_queue.add('is');
Example_queue.add('a');
Example_queue.add('Queue');
print(Example_queue);
}
Here are 10 Dart programs demonstrating the use of the Queue collection. A Queue is a collection designed for efficient insertion and removal of elements at both ends. It's part of the dart:collection library and is typically used in scenarios where you need FIFO (First In, First Out) operations, such as in queues for task scheduling or buffer handling.
To use a Queue, you need to import the dart:collection library.
import 'dart:collection';
void main() {
Queue<String> tasks = Queue<String>();
tasks.add('Task 1');
tasks.add('Task 2');
tasks.add('Task 3');
print('Tasks: $tasks'); // Output: Tasks: {Task 1, Task 2, Task 3}
String currentTask = tasks.removeFirst();
print('Processing: $currentTask'); // Output: Processing: Task 1
print('Remaining tasks: $tasks'); // Output: Remaining tasks: {Task 2, Task 3}
print('First task: ${tasks.first}'); // Output: First task: Task 2
print('Last task: ${tasks.last}'); // Output: Last task: Task 3
tasks.addFirst('Urgent Task');
print('Tasks after adding urgent task: $tasks'); // Output: Tasks after adding urgent task: {Urgent Task, Task 2, Task 3}
tasks.removeLast();
print('Tasks after removing last task: $tasks'); // Output: Tasks after removing last task: {Urgent Task, Task 2}
print('Is queue empty? ${tasks.isEmpty}'); // Output: Is queue empty? false
tasks.clear();
print('Is queue empty after clearing? ${tasks.isEmpty}'); // Output: Is queue empty after clearing? true
}
import 'dart:collection';
void main() {
Queue<String> queue = Queue();
// Enqueue elements
queue.add('Alice');
queue.add('Bob');
queue.add('Charlie');
// Dequeue an element
print("Dequeue: ${queue.removeFirst()}");
// Remaining elements in the queue
print("Queue after dequeue: $queue");
}
import 'dart:collection';
void main() {
Queue<int> queue = Queue();
// Enqueue elements
queue.add(1);
queue.add(2);
queue.add(3);
// Add an element to the front
queue.addFirst(0);
// Add an element to the end
queue.addLast(4);
print("Queue after adding elements: $queue");
}
import 'dart:collection';
void main() {
Queue<int> queue = Queue.from([1, 2, 3, 4, 5]);
// Remove the first element
print("Removed from front: ${queue.removeFirst()}");
// Remove the last element
print("Removed from back: ${queue.removeLast()}");
print("Queue after removals: $queue");
}
import 'dart:collection';
void main() {
Queue<String> queue = Queue.from(['Apple', 'Banana', 'Orange']);
// Iterating through the queue
for (var fruit in queue) {
print(fruit);
}
}
import 'dart:collection';
void main() {
Queue<int> queue = Queue.from([10, 20, 30, 40, 50]);
// Peek the first element without removing it
print("First element: ${queue.first}");
// Peek the last element without removing it
print("Last element: ${queue.last}");
}
import 'dart:collection';
void main() {
Queue<String> queue = Queue.from(['John', 'Paul', 'George']);
if (queue.isEmpty) {
print("The queue is empty.");
} else {
print("The queue has ${queue.length} elements.");
}
// Remove all elements to make it empty
queue.clear();
if (queue.isEmpty) {
print("The queue is now empty.");
}
}
import 'dart:collection';
void main() {
Queue<String> queue = Queue();
// Add multiple elements at once
queue.addAll(['Alice', 'Bob', 'Charlie']);
print("Queue after addAll: $queue");
}
import 'dart:collection';
void main() {
Queue<int> queue = Queue.from([1, 2, 3, 4, 5]);
// Reverse the queue
var reversedQueue = Queue<int>.from(queue.reversed);
print("Reversed Queue: $reversedQueue");
}
import 'dart:collection';
void main() {
Queue<String> taskQueue = Queue();
// Add tasks to the queue
taskQueue.add('Task 1');
taskQueue.add('Task 2');
taskQueue.add('Task 3');
// Process tasks in FIFO order
while (taskQueue.isNotEmpty) {
String task = taskQueue.removeFirst();
print("Processing: $task");
}
}
import 'dart:collection';
void main() {
Queue<int> queue = Queue.from([10, 20, 30]);
// Print the length of the queue
print("Length of the queue: ${queue.length}");
// Add more elements
queue.add(40);
queue.add(50);
// Print the updated length
print("Updated length of the queue: ${queue.length}");
}
add(): Adds an element to the end of the queue.
addFirst(): Adds an element to the front of the queue.
addLast(): Adds an element to the back of the queue.
removeFirst(): Removes and returns the first element from the queue.
removeLast(): Removes and returns the last element from the queue.
first and last: Allows peeking at the first and last elements without removing them.
clear(): Removes all elements from the queue.
isEmpty: Checks whether the queue is empty.
length: Returns the number of elements in the queue.
addAll(): Adds multiple elements to the queue in one operation.
These programs highlight the powerful features of the Queue class in Dart. It's ideal for scenarios where you need a collection that supports efficient first-in, first-out (FIFO) behavior, like task processing or managing buffers.