<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link href="https://unpkg.com/mocha@5.2.0/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha@5.2.0/mocha.js"></script>
<div></div>
</body>
</html>
class Node {
constructor(data) {
this.data = data;
this.next = null
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(data) {
let node = new Node(data)
if (!this.head) {
this.head = node
this.tail = node
} else {
this.tail.next = node;
this.tail = node
}
this.length++
}
pop() {
if (!this.head) return null
let node = this.head;
let deleteNode = node;
if (!node.next) {
this.head = null
this.tail = null
}
while (node.next) {
if (!node.next.next) {
deleteNode = node.next;
node.next = null;
this.tail = node;
break
}
node = node.next
}
this.length--
return deleteNode
}
get(index) {
if (!this.head || index < 0 || index > this.length - 1) return null
let currentNode = this.head;
while (index) {
currentNode = currentNode.next;
index--
}
return currentNode;
}
}
mocha.setup('bdd');
var assert = chai.assert;
describe('Singly Linked list',()=>{
it("should be inserted at the end",()=>{
const list = new SinglyLinkedList();
list.push(1)
list.push(2)
list.push(3)
list.push(4)
assert.equal(4,list.length)
})
it("should be deleted at the end",()=>{
const list = new SinglyLinkedList();
list.push(1)
list.push(2)
list.push(3)
list.push(4)
list.pop();
list.pop();
assert.equal(2,list.length)
})
it("should get the particular index",()=>{
const list = new SinglyLinkedList();
list.push(1)
list.push(6)
list.push(10)
list.push(12)
const node = list.get(2);
assert.equal(10,node.data)
})
})
mocha.run()
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.