From 048c8361b74982e4c866cbafd63930bd0a9193b4 Mon Sep 17 00:00:00 2001 From: Lokesh Jadhav Date: Sun, 14 Jan 2018 11:35:00 +0100 Subject: [PATCH] Add a queue empty check in dequeue function --- data-structures/queue.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/data-structures/queue.js b/data-structures/queue.js index 52d9464..4785377 100644 --- a/data-structures/queue.js +++ b/data-structures/queue.js @@ -68,6 +68,9 @@ Queue.prototype.enqueue = function(value) { // O(1) Queue.prototype.dequeue = function() { + if (this.count() === 0) { + return 'Queue is empty! Enqueue an element before you dequeue'; + } var element = this._storage[this._head]; delete this._storage[this._head]; if (this._head < this._tail) this._head++;