diff --git a/src/dataStructures/Queue.js b/src/dataStructures/Queue.js new file mode 100644 index 0000000..17643b9 --- /dev/null +++ b/src/dataStructures/Queue.js @@ -0,0 +1,56 @@ +class Queue { + constructor() { + this.dataStore = []; + } + + /** + * @name enqueue + * @param {String} elem + * @description + * handler function which will add new element in queue + */ + enqueue(element) { + this.dataStore.push(element); + } + + /** + * @name dequeue + * @description + * handler function which will remove element from queue + * @returns {String} RemoveElement + */ + dequeue() { + return this.dataStore.shift(); + } + + /** + * @name front + * @description + * handler function which provide the first element in queue + * @returns {String} FrontElement + */ + front() { + return this.dataStore[0]; + } + /** + * @name back + * @description + * handler function which provide the last element in queue + * @returns {String} BackElement + */ + back() { + return this.dataStore[this.dataStore.length - 1]; + } + /** + * @name empty + * @description + * handler function which will clear queue + * @returns {Array} EmptyQueue + */ + empty() { + this.dataStore = []; + return this.dataStore; + } +} + +module.exports = Queue; diff --git a/src/dataStructures/Stack.js b/src/dataStructures/Stack.js new file mode 100644 index 0000000..5c3b35b --- /dev/null +++ b/src/dataStructures/Stack.js @@ -0,0 +1,58 @@ +class Stack { + constructor() { + this.top = 0; + this.dataStore = []; + } + /** + * @name push + * @param {String} elem + * @description + * handler function which will add new element in stack and increament top value by 1 + */ + push(element) { + this.dataStore[this.top++] = element; + } + /** + * @name pop + * @param {String} elem + * @description + * handler function which will remove element in stack and decreament top value by 1 + * @returns {String} RemoveElement + */ + pop() { + this.top--; + return this.dataStore.pop() + } + /** + * @name peek + * @param {String} elem + * @description + * handler function which provide the top element on stack + * @returns {String} TopElement + */ + peek() { + return this.dataStore[this.top - 1]; + } + /** + * @name length + * @param {String} elem + * @description + * handler function which provide the length on element + * @returns {Number} + */ + length() { + return this.top; + } + /** + * @name clear + * @description + * handler function which will clear stack + * @returns {Void} + */ + clear() { + this.top = 0; + this.dataStore = [] + } +} + +module.exports = Stack \ No newline at end of file diff --git a/test/dataStructures/Queue.test.js b/test/dataStructures/Queue.test.js new file mode 100644 index 0000000..8c1ae8c --- /dev/null +++ b/test/dataStructures/Queue.test.js @@ -0,0 +1,57 @@ +const Queue = require('../../src/dataStructures/Queue'); + +describe('Queue data structure', () => { + + it ('Should have empty Queue when instantiated', () => { + const queue = new Queue(); + expect(queue.dataStore.length).toBe(0); + }); + + it('Should add the item in last in queue', () => { + const queue = new Queue(); + queue.enqueue(1); + queue.enqueue(2); + expect(queue.back()).toBe(2); + }); + + + it('Should remove items from the front in the Queue', () => { + const queue = new Queue(); + queue.enqueue(1) + queue.enqueue(2) + queue.enqueue(3) + expect(queue.dequeue()).toBe(1); + expect(queue.dequeue()).toBe(2); + expect(queue.dequeue()).toBe(3); + }); + + it('Should print a correct representation of the queue as string', () => { + const queue = new Queue(); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + expect(queue.dataStore.toString()).toBe('1,2,3'); + }); + + it('Should peek front element from queue', () => { + const queue = new Queue(); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + expect(queue.front()).toBe(1); + }); + it('Should peek back element from queue', () => { + const queue = new Queue(); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + expect(queue.back()).toBe(3); + }); + it('Should empty queue', () => { + const queue = new Queue(); + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + expect(queue.empty().length).toBe(0); + }); +}) diff --git a/test/dataStructures/Stack.test.js b/test/dataStructures/Stack.test.js new file mode 100644 index 0000000..89fc1e7 --- /dev/null +++ b/test/dataStructures/Stack.test.js @@ -0,0 +1,43 @@ +const Stack = require('../../src/dataStructures/Stack'); + +describe('Stack data structure', () => { + + it ('Should have empty stack when instantiated', () => { + const stack = new Stack(); + expect(stack.top).toBe(0); + expect(stack.dataStore.length).toBe(0); + }); + + it('Should add the first item on top', () => { + const stack = new Stack(); + stack.push(1); + expect(stack.dataStore.toString()).toBe('1'); + }); + + + it('Should remove items from the top of the stack', () => { + const stack = new Stack(); + stack.push(1) + stack.push(2) + stack.push(3) + expect(stack.pop()).toBe(3); + expect(stack.pop()).toBe(2); + expect(stack.pop()).toBe(1); + }); + + it('Should print a correct representation of the stack as string', () => { + const stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + expect(stack.dataStore.toString()).toBe('1,2,3'); + }); + + it('Should peek top element from stack', () => { + const stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + expect(stack.peek()).toBe(3); + }); +})