diff --git a/spec/linkedList_test.js b/spec/linkedList_test.js index 96ceb4f..0fdab73 100644 --- a/spec/linkedList_test.js +++ b/spec/linkedList_test.js @@ -25,4 +25,128 @@ describe('Linked List', () => { .to.equal('boo') }) }) + + describe('insertFirst()', () => { + it('Inserts a node (with the provided data) to the head of the list', () => { + const linkedList = new LinkedList() + + expect(() => linkedList.insertFirst('foo')) + .to.alter(() => linkedList.count, { from: 0, to: 1 }) + expect(() => linkedList.insertFirst('boo')) + .to.alter(() => linkedList.count, { from: 1, to: 2 }) + expect(linkedList.getHeadNode().data) + .to.equal('boo') + expect(linkedList.getTailNode().data) + .to.equal('foo') + }) + }) + describe('contains()', () => { + it('Determines whether or not the list contains the provided data', () => { + const linkedList = new LinkedList() + linkedList.insertFirst('foo') + linkedList.insertFirst('boo') + + expect(linkedList.contains('foo')) + .to.be.true + expect(linkedList.contains('tap')) + .to.equal(false) + }) + }) + + describe('find()', () => { + it('Determines whether or not the list contains the provided data', () => { + const linkedList = new LinkedList() + linkedList.insertFirst('foo') + linkedList.insertFirst('boo') + + expect(linkedList.find('foo')) + .to.deep.equal({ data: 'foo', next: null }) + expect(linkedList.find('tap')) + .to.equal(-1) + }) + }) + + describe('clear()', () => { + it('Clears the list of all nodes/data', () => { + const linkedList = new LinkedList() + linkedList.insertFirst('foo') + linkedList.insertFirst('boo') + linkedList.insert('shoo') + + expect(linkedList.clear()) + .to.be.undefined + }) + }) + + describe('removeFirst()', () => { + it('Removes the head node from the list', () => { + const linkedList = new LinkedList() + linkedList.insertFirst('foo') + linkedList.insertFirst('boo') + linkedList.insert('shoo') + + expect(linkedList.removeFirst()) + .to.be.undefined + expect(linkedList.getHeadNode().data) + .to.equal('foo') + expect(linkedList.removeFirst()) + .to.be.undefined + expect(linkedList.getHeadNode().data) + .to.equal('shoo') + }) + }) + + describe('remove()', () => { + it('Removes the tail node from the list', () => { + const linkedList = new LinkedList() + linkedList.insertFirst('foo') + linkedList.insertFirst('boo') + linkedList.insert('shoo') + + expect(linkedList.remove()) + .to.be.undefined + expect(linkedList.getTailNode().data) + .to.equal('foo') + expect(linkedList.remove()) + .to.be.undefined + expect(linkedList.getTailNode().data) + .to.equal('boo') + }) + }) + + describe('insertAfter()', () => { + it('Inserts a node (with data) after the first node containing "(item)"', () => { + const linkedList = new LinkedList() + linkedList.insertFirst('foo') + linkedList.insertFirst('boo') + linkedList.insert('shoo') + + expect(linkedList.insertAfter('shoo', 'moo')) + .to.be.undefined + expect(linkedList.getTailNode().data) + .to.equal('moo') + expect(linkedList.insertAfter('moo', 'doo')) + .to.be.undefined + expect(linkedList.getTailNode().data) + .to.equal('doo') + }) + }) + + describe.only('insertBefore()', () => { + it('Inserts a node (with data) before the first node containing (item)', () => { + const linkedList = new LinkedList() + linkedList.insertFirst('foo') + linkedList.insertFirst('boo') + linkedList.insert('shoo') + + expect(linkedList.insertBefore('shoo', 'moo')) + .to.be.undefined + expect(linkedList.getTailNode().data) + .to.equal('shoo') + expect(linkedList.insertBefore('shoo', 'doo')) + .to.be.undefined + expect(linkedList.getTailNode().data) + .to.equal('shoo') + }) + }) }) diff --git a/spec/set_test.js b/spec/set_test.js index 65a2432..4e227b6 100644 --- a/spec/set_test.js +++ b/spec/set_test.js @@ -157,6 +157,4 @@ describe('Set', () => { .to.deep.equal(['A', 'B', 'C']) }) }) - ->>>>>>> 6b6e0bc454643ca2afa1a9c01e423760549881d0 }) diff --git a/src/linkedList.js b/src/linkedList.js index 3be005c..0b69a71 100644 --- a/src/linkedList.js +++ b/src/linkedList.js @@ -22,16 +22,28 @@ export default class LinkedList { return this.tail } - contains() { + find(item) { + let currentNode = this.head + while (currentNode) { + if (currentNode.data === item) { + return currentNode + } else { + currentNode = currentNode.next + } + } + return -1 } - find() { - + clear() { + this.head = null + this.tail = null + this.count = 0 } insert(item) { const newNode = new Node(item) + if (this.head === null) { this.head = newNode } else { @@ -39,45 +51,93 @@ export default class LinkedList { } this.tail = newNode this.count++ - // if (this.count = 0){ - // this.head = newNode - // this.tail = newNode - // } else { - // this.tail = newNode - // } + return newNode } - insertFirst() { + insertFirst(item) { + const newNode = new Node(item) + + if (this.head) { + newNode.next = this.head + } else { + this.tail = newNode + } + + this.head = newNode + this.count++ + + return newNode } - insertBefore() { + insertBefore(item, data) { + const node = new Node(data) + node.next = this.find(item) + + if (node.next === this.getHeadNode()) { + this.head = node + } else { + let currentNode = this.getHeadNode() + while (currentNode.next !== node.next) { + currentNode = currentNode.next + } + currentNode.next = node + } + this.count++ } - insertAfter() { + insertAfter(item, data) { + const node = new Node(data) + if (this.find(item) === this.getTailNode()) { + this.tail = node + } + node.next = this.find(item).next + this.find(item).next = node + this.count++ } remove() { + if (this.count === 1) { + this.clear() + } else { + let currentNode = this.getHeadNode() + while (currentNode.next.next) { + currentNode = currentNode.next + } + currentNode.next = null + this.count-- + this.tail = currentNode + } } removeFirst() { + if (this.count === 1) { + this.clear() + } else { + this.head = this.getHeadNode().next + this.count-- + } } isEmpty() { - + if(!this.head){ + return null + } else { + return this.data + } } size() { - + return this.count } - clear() { - + contains(item) { + return this.find(item) !== -1 } }