From 42998ace8593cb803aa47b72a461d1a540842e1e Mon Sep 17 00:00:00 2001 From: Josh Vinge Date: Thu, 28 Sep 2017 10:52:12 -0400 Subject: [PATCH 1/2] Added Queue Data Structure --- Data Structures/Queue/Queue.js | 97 ++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Data Structures/Queue/Queue.js diff --git a/Data Structures/Queue/Queue.js b/Data Structures/Queue/Queue.js new file mode 100644 index 0000000000..bf5f5a3958 --- /dev/null +++ b/Data Structures/Queue/Queue.js @@ -0,0 +1,97 @@ +/* Queue +* A Queue is a data structure that allows you to add an element to the end of +* a list and remove the item at the front. A queue follows a "First In First Out" +* system, where the first item to enter the queue is the first to be removed. This +* implementation uses an array to store the queue. +*/ + +//Functions: enqueue, dequeue, peek, view, length + +var Queue = function() { + + //This keeps track of where the end of the queue is + this.back = 0; + //This is the array representation of the queue + this.queue = {}; + + //Add a value to the end of the queue + this.enqueue = function(item) { + this.queue[this.back] = item; + this.back++; + } + + //Removes the value at the front of the queue + this.dequeue = function() { + if (this.back === 0) { + return "Queue is Empty"; + } + + var result = this.queue[this.front]; + delete this.queue[this.front]; + + //Shift all the other items forward + for (var i = 1; i < this.back; i++) { + this.queue[i - 1] = this.queue[i]; + } + + //clean up the leftover duplicated value at the back of the queue + delete this.queue[this.back]; + this.back--; + + return result; + } + + //Return the length of the queue + this.length = function() { + return this.back; + } + + //Return the item at the front of the queue + this.peek = function() { + return this.queue[0]; + } + + //List all the items in the queue + this.view = function() { + var str = "{" + //construct a single string to represent the items in the queue + for (var i = 0; i < this.back; i++) { + str += this.queue[i]; + if (i !== this.back - 1) { + str += ", "; + } + } + str += "}"; + + console.log(str); + } +} + +//Implementation +var myQueue = new Queue(); + +myQueue.enqueue(1); +myQueue.enqueue(5); +myQueue.enqueue(76); +myQueue.enqueue(69); +myQueue.enqueue(32); +myQueue.enqueue(54); + +myQueue.view(); + +console.log("Length: " + myQueue.length()); +console.log("Front item: " + myQueue.peek()); +console.log("Removed " + myQueue.dequeue() + " from front."); +console.log("New front item: " + myQueue.peek()); +console.log("Removed " + myQueue.dequeue() + " from front."); +console.log("New front item: " + myQueue.peek()); +myQueue.enqueue(55); +console.log("Inserted 55"); +console.log("New front item: " + myQueue.peek()); + +for (var i = 0; i < 5; i++) { + myQueue.dequeue(); + myQueue.view(); +} + +console.log(myQueue.dequeue()); \ No newline at end of file From 11964de6b071c84662ce4369a49eb8e700a9e380 Mon Sep 17 00:00:00 2001 From: Josh Vinge Date: Sun, 1 Oct 2017 20:24:25 -0400 Subject: [PATCH 2/2] Changed queue from object type to array type and adjusted methods to reflect this change --- Data Structures/Queue/Queue.js | 36 +++++++--------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/Data Structures/Queue/Queue.js b/Data Structures/Queue/Queue.js index bf5f5a3958..e401b7ae71 100644 --- a/Data Structures/Queue/Queue.js +++ b/Data Structures/Queue/Queue.js @@ -9,41 +9,29 @@ var Queue = function() { - //This keeps track of where the end of the queue is - this.back = 0; //This is the array representation of the queue - this.queue = {}; + this.queue = []; //Add a value to the end of the queue this.enqueue = function(item) { - this.queue[this.back] = item; - this.back++; + this.queue[this.queue.length] = item; } //Removes the value at the front of the queue this.dequeue = function() { - if (this.back === 0) { + if (this.queue.length === 0) { return "Queue is Empty"; } - var result = this.queue[this.front]; - delete this.queue[this.front]; - - //Shift all the other items forward - for (var i = 1; i < this.back; i++) { - this.queue[i - 1] = this.queue[i]; - } - - //clean up the leftover duplicated value at the back of the queue - delete this.queue[this.back]; - this.back--; + var result = this.queue[0]; + this.queue.splice(0, 1); //remove the item at position 0 from the array return result; } //Return the length of the queue this.length = function() { - return this.back; + return this.queue.length; } //Return the item at the front of the queue @@ -53,17 +41,7 @@ var Queue = function() { //List all the items in the queue this.view = function() { - var str = "{" - //construct a single string to represent the items in the queue - for (var i = 0; i < this.back; i++) { - str += this.queue[i]; - if (i !== this.back - 1) { - str += ", "; - } - } - str += "}"; - - console.log(str); + console.log(this.queue); } }