algorithms_and_data_structures
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "algorithms_and_data_structures", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Algorithms and data structures in javascript.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
'use strict' | ||
function Node(data, next) { | ||
this._data = data; | ||
this._next = next; | ||
} | ||
let Node = { | ||
init(data, next) { | ||
this.data = data; | ||
this.next = next; | ||
return this; | ||
} | ||
}; | ||
function LinkedList() { | ||
this._head = null; | ||
this._tail = null; | ||
this._length = 0; | ||
} | ||
LinkedList.prototype.pushFirst = function pushFirst(data) { | ||
var n = new Node(data, this._head); | ||
this._head = n; | ||
if(this._length == 0) | ||
this._tail = n; | ||
this._length++; | ||
} | ||
LinkedList.prototype.popFirst = function popFirst() { | ||
if(this._length == 0) | ||
return null; | ||
var d = this._head._data; | ||
this._head = this._head._next; | ||
if(--this._length == 0) | ||
module.exports = { | ||
init() { | ||
this._length = 0; | ||
this._head = null; | ||
this._tail = null; | ||
return d; | ||
} | ||
return this; | ||
}, | ||
LinkedList.prototype.pushLast = function pushLast(data) { | ||
var n = new Node(data, null); | ||
if(this._length == 0) | ||
pushFirst(data) { | ||
let n = Object.create(Node).init(data, this._head); | ||
this._head = n; | ||
else | ||
this._tail._next = n; | ||
this._tail = n; | ||
this._length++; | ||
} | ||
if(this._length == 0) | ||
this._tail = n; | ||
this._length++; | ||
}, | ||
LinkedList.prototype.reverse = function reverse() { | ||
if(this._length == 0) | ||
return; | ||
this._tail = this._head; | ||
var prev = null; | ||
var next = this._head._next; | ||
popFirst() { | ||
if(this._length == 0) | ||
return null; | ||
let d = this._head.data; | ||
this._head = this._head.next; | ||
if(--this._length == 0) | ||
this._tail = null; | ||
return d; | ||
}, | ||
while(true) { | ||
this._head._next = prev; | ||
prev = this._head; | ||
if(next == null) | ||
break | ||
this._head = next; | ||
next = next._next; | ||
pushLast(data) { | ||
let n = Object.create(Node).init(data, null); | ||
if(this._length == 0) | ||
this._head = n; | ||
else | ||
this._tail.next = n; | ||
this._tail = n; | ||
this._length++; | ||
}, | ||
} | ||
} | ||
reverse() { | ||
if(this._length == 0) | ||
return; | ||
this._tail = this._head; | ||
let prev = null; | ||
let next = this._head.next; | ||
LinkedList.prototype[Symbol.iterator] = function iterator() { | ||
var curr = undefined; | ||
return { | ||
next: () => { | ||
curr = (curr == undefined) ? this._head : curr._next; | ||
return { | ||
value: curr, | ||
done: (curr == undefined) | ||
while(true) { | ||
this._head.next = prev; | ||
prev = this._head; | ||
if(next === null) | ||
break | ||
this._head = next; | ||
next = next.next; | ||
} | ||
}, | ||
[Symbol.iterator]() { | ||
let curr = undefined; | ||
return { | ||
next: () => { | ||
curr = (curr == undefined) ? this._head : curr.next; | ||
return { | ||
value: curr, | ||
done: (curr == undefined) | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
module.exports = LinkedList; |
'use strict' | ||
const LinkedList = require('../src/linked_list.js'); | ||
const LinkedList = require('../src/linked_list'); | ||
//const process = require('process'); | ||
@@ -9,3 +9,3 @@ | ||
test('stack', () => { | ||
var stack = new LinkedList(); | ||
var stack = Object.create(LinkedList).init(); | ||
stack.pushFirst('hello'); | ||
@@ -20,3 +20,3 @@ stack.pushFirst('sweet'); | ||
test('queue', () => { | ||
var queue = new LinkedList(); | ||
var queue = Object.create(LinkedList).init(); | ||
queue.pushLast('hello'); | ||
@@ -31,3 +31,3 @@ queue.pushLast('sweet'); | ||
test('reverse stack', () => { | ||
var stack = new LinkedList(); | ||
var stack = Object.create(LinkedList).init(); | ||
stack.pushFirst('hello'); | ||
@@ -37,3 +37,2 @@ stack.pushFirst('sweet'); | ||
stack.reverse(); | ||
expect(stack.popFirst()).toBe('hello'); | ||
@@ -47,3 +46,3 @@ expect(stack.popFirst()).toBe('sweet'); | ||
/* | ||
var q = new LinkedList(); | ||
var q = Object.create(LinkedList).init(); | ||
q.pushLast('a'); | ||
@@ -65,3 +64,3 @@ q.pushLast('b'); | ||
/* | ||
var q = new LinkedList(); | ||
var q = Object.create(LinkedList).init(); | ||
var a = []; | ||
@@ -86,3 +85,3 @@ | ||
/* | ||
var s = new LinkedList(); | ||
var s = Object.create(LinkedList).init(); | ||
var a = []; | ||
@@ -89,0 +88,0 @@ |
Sorry, the diff of this file is not supported yet
5431
150