Skip to content
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
33 changes: 33 additions & 0 deletions spec/linkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import chai, { expect } from 'chai'
import chaiChange from 'chai-change'
// import Node from '../src/node'
import LinkedList from '../src/linkedList'

chai.use(chaiChange)

describe('LinkedList', function() {
'use strict'

it('exists', function() {
expect(LinkedList).to.be.a('function')
})

context('getHeadNode()', function() {
it('returns null when there is no head node', function() {
const myLinkedList = new LinkedList()
expect(myLinkedList.getHeadNode()).to.equal(null)
})

it('returns the head node', function() {
const myLinkedList = new LinkedList()
myLinkedList.insertFirst('hello')

expect(myLinkedList.getHeadNode().getData()).to.equal('hello')
})
})


})

// expect(() => myLinkedList.push('foo'))
// .to.alter(() => myLinkedList.length(), { from: 0, to: 1 })
48 changes: 48 additions & 0 deletions spec/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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('getData()', function() {
it('returns the node\'s data', function() {
const myNode = new Node({data: 'hello'})

expect(myNode.getData()).to.equal('hello')
})
})

context('getNext()', function() {
it('returns null if next node is not set', function() {
const myNode = new Node({data: 'hello'})

expect(myNode.getNext())
.to.equal(null)
})

it('returns next node if set', function() {
const aNode = new Node({data: 'world'})
const myNode = new Node({data: 'hello', next: aNode })

expect(myNode.getNext())
.to.equal(aNode)
})
})

context('setNext()', function() {
it('sets next node', function() {
const myNode = new Node({data: 'hello'})
const newNode = new Node({data: 'world'})

expect(() => myNode.setNext(newNode))
.to.alter(() => myNode.getNext(), { from: null, to: newNode })
})
})
})
154 changes: 154 additions & 0 deletions src/linkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
'use strict'

import Node from './node';

export default class linkedList {
constructor() {
this.headNode = null;
this.tailNode = null;
this.size = 0;
}

getHeadNode() {
return this.headNode;
}

getTailNode() {
return this.tailNode;
}

contains(data) {
let node = this.headNode;

while(true) {
if (node.getData() === data) {
return true;
}

if (node === this.tailNode) {
return false;
}

node = node.getNext();
}
}

find(data) {
let node = this.headNode;

while(true) {
if (node.getData() === data) {
return node;
}

if (node === this.tailNode) {
return -1;
}

node = node.getNext();
}
}

insert(data) {
const node = new Node({ data });

if (this.tailNode) {
this.tailNode.setNext(node);
}

this.tailNode = node;
this.size++;
}

insertFirst(data) {
const node = new Node({ data });

if (this.headNode) {
node.setNext(this.headNode);
}

this.headNode = node;
this.size++;
}

insertBefore(reference, data) {
const node = new Node({ data });
let currentNode = this.headNode.getNext();
let previousNode = this.headNode;
let inserted = false;

if (this.headNode.getData() === reference) {
node.setNext(this.headNode);
this.headNode = node;
}

while(!inserted) {
if (!currentNode) {
return -1;
}

if (currentNode.getData() === reference) {
previousNode.setNext(node);
node.setNext(currentNode);
inserted = true;
}

previousNode = currentNode;
currentNode = currentNode.getNext();
}

this.size++;
}

insertAfter(reference, data) {
const node = new Node({ data });
let currentNode = this.headNode;
let nextNode = currentNode.getNext();
let inserted = false;

while(!inserted) {
if (currentNode.getData() === reference) {
currentNode.setNext(node);
node.setNext(nextNode);
inserted = true;
}

if (!nextNode) {
return -1;
}

currentNode = nextNode;
nextNode = nextNode.getNext();
}
}

remove() {
let node = this.headNode;

while(node.getNext() !== this.tailNode) {
node = node.getNext();
}

node.setNext(null);
this.tailNode = node;
}

removeFirst() {
const newHead = this.headNode.getNext();
this.headNode = newHead;
}

isEmpty() {
return this.size === 0;
}

size() {
return this.size;
}

clear() {
this.headNode = null;
this.tailNode = null;
this.size = 0;
}
}
21 changes: 21 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

export default class Node {
constructor({ data, next }) {
this.data = data;
this.next = next || null;
}

getData() {
return this.data;
}

getNext() {
return this.next;
}

setNext(nextNode) {
this.next = nextNode;
return this;
}
}