Sitemap
Arya P

Arya P

Queue using Dart

Jun 23, 2022

A queue is linear data structure that follow a first-in-first-out sequence. This implies that the first item to be inserted will be the first to be removed. You can also say that items are removed in the order they were inserted.

Get Arya P’s stories in your inbox

Join Medium for free to get updates from this writer.

Queue operations:

  1. Enqueue: Inserts an element into the queue.
  2. Dequeue: Removes an element from the queue.

void main() {
final queue = Queue<int>();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);

print(queue);

final element = queue.dequeue();
print(‘dequeue: $element’);
print(“after dequeue an element\n $queue”);

}

class Queue<E> {
final List<E> list = [];

@override
String toString() {
return
‘${list}’;

}
void enqueue(E element) => list.add(element);

bool get isEmpty => list.isEmpty;

E dequeue() =>(isEmpty)?throw “queue underflow”: list.removeAt(0);

}

--

--