Skip to content
Merged
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
124 changes: 124 additions & 0 deletions spec/linkedList_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
2 changes: 0 additions & 2 deletions spec/set_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,4 @@ describe('Set', () => {
.to.deep.equal(['A', 'B', 'C'])
})
})

>>>>>>> 6b6e0bc454643ca2afa1a9c01e423760549881d0
})
92 changes: 76 additions & 16 deletions src/linkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,122 @@ 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 {
this.tail.next = newNode
}
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
}

}