diff --git a/spec/linkedList.js b/spec/linkedList.js new file mode 100644 index 0000000..1c5f8c9 --- /dev/null +++ b/spec/linkedList.js @@ -0,0 +1,187 @@ +import chai, { expect } from 'chai' +import chaiChange from 'chai-change' +import LinkedList from '../src/linkedList' + +chai.use(chaiChange) + +describe('LinkedList', () => { + + + it('exists', () => { + + expect(LinkedList).to.be.a('function') + + }) + + context('getHeadNode', () => { + + it('returns head node', () => { + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insert('banana') + + expect(linkedList.getHeadNode().data).to.equal('apple') + + }) + }) + + context('getTailNode', () => { + + it('returns tail node', () => { + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insert('banana') + + expect(linkedList.getTailNode().data).to.equal('banana') + + }) + }) + + context('contains', () => { + const string = 'banana' + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insert('banana') + + it('checks to see if node in list contains "bananas"', () => { + + expect(linkedList.contains("banana")).to.equal(true) + + }) + }) + + context('find', () => { + const string = 'banana' + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insert('banana') + + it('returns the first node with corresponding data or -1', () => { + + expect(linkedList.find("banana").data).to.equal('banana') + + }) + }) + + context('insert', () => { + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insert('banana') + + it.only('inserts node with data to tail of list', () => { + + expect(linkedList.tail.data).to.equal("banana") + + }) + }) + + context('insertFirst', () => { + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insertFirst('banana') + + it('inserts node with data at head of list', () => { + + expect(linkedList.head.data).to.equal("banana") + + }) + }) + + context('insertBefore', (string, data) => { + const linkedList = new LinkedList() + linkedList.insert('banana') + linkedList.insertBefore('banana', 'apple') + + it('inserts node containing "apple" before node containing "banana"', () => { + + expect(linkedList.head.data).to.equal('apple') + + }) + }) + + context('insertAfter', () => { + const linkedList = new LinkedList() + linkedList.insert('banana') + linkedList.insert('twitter') + linkedList.insert('google') + linkedList.insert('netflix') + linkedList.insertAfter('banana', 'apple') + + + it('inserts node containing "bananas" after node containing "apples"', () => { + + expect(linkedList.head.next.data).to.equal('apple') + + }) + }) + + context('remove', () => { + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insert('banana') + linkedList.remove() + + it('removes the tail node from the list', () => { + + expect(linkedList.tail.data).to.equal('apple') + + }) + }) + + context('removeFirst', () => { + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insert('banana') + linkedList.removeFirst() + + it('removes the head element from list', () => { + + expect(linkedList.head.data).to.equal('banana') + }) + }) + + context('isEmpty', () => { + const linkedList = new LinkedList() + linkedList.isEmpty() + + + + it('Determines if list is empty or not', () => { + + expect(linkedList.isEmpty()).to.equal(true) + + + }) + }) + + context('size', () => { + + it('Determines the number of nodes in list', () => { + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insert('banana') + linkedList.insert('tree') + linkedList.insert('grapes') + linkedList.insert('fire') + expect(linkedList.size).to.equal(5) + }) + }) + + context('clear', () => { + const linkedList = new LinkedList() + linkedList.insert('apple') + linkedList.insert('banana') + linkedList.insert('tree') + linkedList.insert('grapes') + linkedList.insert('fire') + linkedList.clear() + + it('clears the list of all nodes/data', () => { + + expect(linkedList.tail).to.equal(null) && + expect(linkedList.head).to.equal(null) && + expect(linkedList.size).to.equal(0) + + }) + }) +}) diff --git a/spec/node.js b/spec/node.js new file mode 100644 index 0000000..167c8b8 --- /dev/null +++ b/spec/node.js @@ -0,0 +1,29 @@ +import chai, { expect } from 'chai' +import chaiChange from 'chai-change' +import Node from '../src/node' + +chai.use(chaiChange) + +describe('Node', () => { + 'use strict' + + it('exists', () => { + expect(Node).to.be.a('function') + }) + + context('setNext()', () => { + it('Changes the next node in queue and returns current node', () => { + +const nodeA = new Node({data: "apple"}) +const nodeB = new Node({data: "banana"}) + + expect(() => nodeA.setNext(nodeB)) + .to.alter(() => nodeA.next, { from: null, to: nodeB }) + }) + context('getNext()', () => { + it('returns the value of the next object in queue', () => { + expect(() => nodeA.getNext().to.equal({nodeB})) + }) + }) + }) +}) diff --git a/spec/queue.js b/spec/queue.js new file mode 100644 index 0000000..55d3811 --- /dev/null +++ b/spec/queue.js @@ -0,0 +1,28 @@ +import chai, { expect } from 'chai' +import chaiChange from 'chai-change' +import Queue from '../src/queue' + +chai.use(chaiChange) + +describe('Queue', () => { + + it('exists', () => { + + expect(Queue).to.be.a('function') + + }) + + context('enqueue', () => { + const queue = new Queue() + queue.enqueue('dumbass') + queue.enqueue('Hella Weed') + queue.enqueue('Dummy Weed') + + it('adds node containing string to the back of queue', () => { + + expect(queue.size).to.equal(3) + }) + }) + + context('dequeue') +}) diff --git a/spec/stack.js b/spec/stack.js index 743122a..abe2ead 100644 --- a/spec/stack.js +++ b/spec/stack.js @@ -8,10 +8,12 @@ describe('Stack', () => { 'use strict' it('exists', () => { + expect(Stack).to.be.a('function') }) context('push()', () => { + it('pushes an element to the top of the stack.', () => { const myStack = new Stack() @@ -19,4 +21,51 @@ describe('Stack', () => { .to.alter(() => myStack.length(), { from: 0, to: 1 }) }) }) + + context('pop()', () => { + const myStack = new Stack() + myStack.push('indica') + myStack.push('sativa') + myStack.push('alcohol') + myStack.pop() + + it('removes the top element from stack', () =>{ + + expect(myStack.size).to.equal(2) + }) + }) + + context('peek()', () => { + const myStack = new Stack() + myStack.push('indica') + myStack.push('sativa') + myStack.peek() + + it('pushes an element', () => { + + expect(myStack.top.data).to.equal('sativa') + }) + }) + + context('isEmpty', () => { + const myStack = new Stack() + + it('Returns true if stack is empty', () => { + + expect(myStack.isEmpty()).to.equal(true) + }) + }) + + context('length()', () => { + const myStack = new Stack() + myStack.push('indica') + myStack.push('sativa') + myStack.length() + + it('Returns the number of nodes/size of stack', () => { + + expect(myStack.length()).to.equal(2) + }) + }) + }) diff --git a/src/linkedList.js b/src/linkedList.js new file mode 100644 index 0000000..a081971 --- /dev/null +++ b/src/linkedList.js @@ -0,0 +1,145 @@ +"use strict" +import Node from '../src/node' +export default class LinkedList { + constructor(data){ + this.head = null + this.tail = null + this.size = 0 + + } + + getHeadNode(){ + if (!this) { + return null + } + return this.head + } + + getTailNode(){ + if (!this) { + return null + } + return this.tail + } + + contains(string){ + let current = this.head + if (this.head.data == string) { + return true + } + current = this.head.next + if (current.data == string) { + return true + } + if (current.data !== string && current.next == null) { + return false + } + } + + + find(string){ + let current = this.head + while(current.data !== string){ + current = current.next + } + if (current.data == string) { + return current + } + return -1 + } + + insert(data){ + const current = new Node(data) + this.size ++ + if (!this.head) { + this.head = current + } + else { + this.tail.next = current + this.tail = current + } + this.tail = current + return + } + + insertFirst(data){ + const current = new Node(data) + this.size ++ + if (!this.head){ + this.head = current + this.tail = current + } + this.head = this.head.next + this.head = current + + } + + insertBefore(string, data){ + this.size ++ + let stringNode = this.find(string) + let thisNode = this.head + let newNode = new Node(data) + if (thisNode.data == string){ + this.head = newNode + this.head.next = stringNode + this.tail = thisNode + } else { + while (thisNode.next.data !== string) { + thisNode = thisNode.next + } + if (thisNode.data === string) { + thisNode.next = newNode + newNode.next.data = string + } + } +} +insertAfter(string, data){ + let newNode = new Node(data) + this.size ++ + let stringNode = this.find(string) + let thisNode = this.head + while (thisNode !== stringNode){ + thisNode = thisNode.next + } + newNode.next = stringNode + thisNode.next = newNode +} + + clear(){ + this.head = null + this.tail = null + this.size = 0 + return this + } + remove(){ + this.size -- + let findTail = this.head + if(findTail == this.tail){ + this.head = null + this.tail = null + } + if(findTail !== this.tail) + while (findTail.next.next !== null){ + findTail = findTail.next + } + findTail.next = null + this.tail = findTail + } + removeFirst(){ + this.size -- + if (!this.head){ + return null + } + if (!this.head.next) { + this.head = null + } + let newHead = this.head.next + this.head = newHead + } + isEmpty() { + if (this.head === null && this.tail === null && this.size === 0) { + return true + } + return false + } +} diff --git a/src/node.js b/src/node.js new file mode 100644 index 0000000..ea9b96b --- /dev/null +++ b/src/node.js @@ -0,0 +1,21 @@ +'use strict' + +export default class Node{ + constructor(data) { + this.data = data + this.next = null + + } + + setNext(data) { + this.next = data + return this + } + + getNext() { + if (this.next) { + return this.next + } + return null + } +} diff --git a/src/queue.js b/src/queue.js new file mode 100644 index 0000000..976e000 --- /dev/null +++ b/src/queue.js @@ -0,0 +1,46 @@ +'use strict' + +import Node from '../src/node' +export default class Queue { + constructor(data){ + this.head = null + this.next = null + this.tail = null +} + + enqueue(string) { + const current = new Node(string) + this.size ++ + if (!this.head) { + this.head = current + } + else { + this.tail.next = current + this.tail = current + } + this.tail = current + return + } + + dequeue() { + + } + + front() { + + } + + back() { + + } + + isEmpty() { + + } + + length() { + + } + + +} diff --git a/src/stack.js b/src/stack.js index dcd1d13..5ff5ea2 100644 --- a/src/stack.js +++ b/src/stack.js @@ -1,5 +1,40 @@ 'use strict' +import Node from '../src/node' export default class Stack { - // your code here + constructor(data){ + this.top = null + this.size = 0 + + } + + push(data){ + this.size ++ + let newNode = new Node(data) + newNode.next = this.top + this.top = newNode + + } + + pop(){ + this.size -- + this.top = this.top.next + + } + + peek(){ + return this.top + + } + + isEmpty(){ + if(this.top === null){ + return true + } + return false + } + + length(){ + return this.size + } }