diff --git a/lib/problems.js b/lib/problems.js index 0c3ab21..bcbc3dc 100644 --- a/lib/problems.js +++ b/lib/problems.js @@ -1,9 +1,27 @@ +const Stack = require("./stack") + /* -Time Complexity: ? -Space Complexity: ? +Time Complexity: O(n) +Space Complexity: O(n) */ const balanced = (str) => { - throw new Error("This method has not been implemented!"); + var symbols = { + "{":"}", + "[":"]", + "(":")", + } + + const stack = new Stack(); + const closings = Object.values(symbols); + if (str.length % 2 !== 0) { return false } + for (let i = 0; i < str.length; i++) { + if (str[i] in symbols) { // push to stack since it's a key + stack.push(str[i]) + } else if (closings.includes(str[i])) { + if (symbols[stack.pop()] !== str[i]) { return false } + } + } + return stack.isEmpty() } /* diff --git a/lib/queue.js b/lib/queue.js index 7a653f8..4ad70d3 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -1,27 +1,35 @@ class Queue { - constructor() { - // this.store = ... - throw new Error("This method has not been implemented!"); + constructor(size = 20) { + this.store = new Array(size); + this.head = 0; + this.contains = 0; } enqueue(element) { - throw new Error("This method has not been implemented!"); + if (this.contains === 20) { throw new Error("The queue is full"); } + this.store[(this.head+this.contains)%this.store.length] = element; + this.contains++; } dequeue() { - throw new Error("This method has not been implemented!"); + if (this.contains === 0) { throw new Error("The queue is empty"); } + let element = this.store[this.head]; + this.store[this.head] = null; + this.head = ((this.head % this.store.length) + 1) % this.contains; + this.contains--; + return element; } front() { - throw new Error("This method has not been implemented!"); + return this.store[this.head] } size() { - throw new Error("This method has not been implemented!"); + return this.contains } isEmpty() { - throw new Error("This method has not been implemented!"); + return (this.contains === 0) } toString() { diff --git a/lib/stack.js b/lib/stack.js index dff9f6c..c134d70 100644 --- a/lib/stack.js +++ b/lib/stack.js @@ -1,19 +1,22 @@ +const LinkedList = require("./linked-list"); + class Stack { constructor() { - // this.store = ... - throw new Error("This method has not been implemented!"); + this.store = new LinkedList(); } - push() { - throw new Error("This method has not been implemented!"); + push(element) { + this.store.addFirst(element); } pop() { - throw new Error("This method has not been implemented!"); + let first = this.store.getFirst(); + this.store.delete(first); + return first; } isEmpty() { - throw new Error("This method has not been implemented!"); + return this.store.isEmpty(); } toString() { diff --git a/test/queue.test.js b/test/queue.test.js index 353f117..753ae83 100644 --- a/test/queue.test.js +++ b/test/queue.test.js @@ -101,4 +101,29 @@ describe("test queue implementation", () => { expect(q.toString()).toEqual('[40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210]'); }); + it('throws error for adding to full queue', () => { + const q = new Queue(); + for (let i = 0; i < 20; i++) { + q.enqueue(i); + } + expect(q.size()).toEqual(20) + expect(() => { + q.enqueue(100) + }).toThrow("The queue is full"); + }); + + it('is a circular queue', () => { + const q = new Queue(); + for (let i = 0; i < 20; i++) { + q.enqueue(i); + } + expect(q.size()).toEqual(20) + q.dequeue() + q.dequeue() + expect(q.size()).toEqual(18) + let i = q.dequeue() + expect(i).toEqual(2) + q.enqueue(100) + expect(q.store[0]).toEqual(100) + }); });