Skip to content

add Queue data structure implementation and test suits #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/dataStructures/Queue.js
Original file line number Diff line number Diff line change
@@ -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;
58 changes: 58 additions & 0 deletions src/dataStructures/Stack.js
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions test/dataStructures/Queue.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
})
43 changes: 43 additions & 0 deletions test/dataStructures/Stack.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
})