class Node {
	constructor(value) {
		this.value = value;
		this.next = null;
	}
}

class Queue {
	constructor() {
		this.head = null;
		this.tail = null;
		this.size = 0;
	}
	
	enqueue(value) {
		if (this.head === null) {
			this.head = new Node(value);
			this.tail = this.head;
			this.size++;
			return;
		}
		
		this.size++;
		this.tail.next = new Node(value);
		this.tail = this.tail.next;
	}
	
	dequeue() {
		if (this.head === null) return;
		if (this.head.next === null) {
			this.head = null;
			this.tail = null;
			this.size--;
			return;
		}
		
		
		this.head = this.head.next;		
		this.size--;
	}
	
	isEmpty() {
		return this.head === null ? true : false;
	}
	
	get length() {
		return this.size;
	}

	clear() {
		this.head = null;
		this.tail = null;
		this.size = 0;
	}
}

+ Recent posts