// FIFO
class Node{
late dynamic value;
late dynamic next;
Node(value){
this.value = value;
this.next = null;
}
}
class Queue{
late dynamic first;
late dynamic last;
late int length;
Queue(){
this.first = null;
this.last = null;
this.length = 0;
}
// add at the back of last
enQueue(value){
Node newNode = new Node(value);
if(length == 0){
first = newNode;
last = first;
}else{
last.next = newNode;
last = newNode;
}
length++;
}
// remove the first push
deQueue(){
if(first == last){
return first;
} else if(first == null){
return null;
}else{
first = first.next;
}
length--;
}
// return the top;
peek(){
return first!= null? first.value.toString(): 'empty';
}
@override
String toString(){
List<dynamic> values = [];
Node? current = first;
while (current != null) {
values.add({
'value': current.value,
'next': current.next?.value,
}); // Add only values
current = current.next;
}
'length': length,
'elements': values
} .toString();
}
}
void main() {
Queue queue = new Queue();
queue.enQueue(23);
queue.enQueue(25);
queue.enQueue(26);
// queue.enQueue(27);
// queue.enQueue(28);
// queue.enQueue(29);
// queue.enQueue(24);
print(queue);
queue.deQueue();
print( queue.peek());
print(queue);
}