function AnimalShelter() {
this.size = 0;
this.first = this.last = null;
}
function Node(data) {
this.data = data;
this.next = null;
}
AnimalShelter.prototype.enqueue = function(data) {
const node = new Node(data);
if (this.last) this.last.next = node;
this.last = node;
if (!this.first) this.first = this.last;
this.size++;
return node;
};
AnimalShelter.prototype.dequeueAny = function() {
if (!this.first) return;
const data = this.first.data;
this.first = this.first.next;
if (!this.first) this.last = null;
this.size = Math.max(0, this.size - 1);
return data;
};
AnimalShelter.prototype.dequeueDog = function() {
let prev = current = this.first;
let next = current ? current.next : null;
let dog;
while (current) {
if (current.data === 'dog') {
if (this.first === current && next === null) {
this.first = this.last = null;
} else if (next === null && prev) {
this.last = prev;
}
dog = current.data;
prev.next = next;
this.size--;
break;
}
prev = current;
current = next;
next = current ? current.next : null;
}
return dog;
};
AnimalShelter.prototype.dequeueCat = function() {
let prev = current = this.first;
let next = current ? current.next : null;
let cat;
while (current) {
if (current.data === 'cat') {
if (this.first === current && next === null) {
this.first = this.last = null;
} else if (next === null && prev) {
this.last = prev;
}
cat = current.data;
prev.next = next;
this.size--;
break;
}
prev = current;
current = next;
next = current ? current.next : null;
}
return cat;
};
AnimalShelter.prototype.peek = function() {
if (!this.first) return;
return this.first.data;
};
AnimalShelter.prototype.isEmpty = function() {
return this.size === 0 && !this.first;
};
AnimalShelter.prototype.getSize = function() {
return this.size;
}