algorithms_and_data_structures
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "algorithms_and_data_structures", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Algorithms and data structures in javascript.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -43,2 +43,20 @@ | ||
LinkedList.prototype.reverse = function reverse() { | ||
if(this._length == 0) | ||
return; | ||
this._tail = this._head; | ||
var prev = null; | ||
var next = this._head._next; | ||
while(true) { | ||
this._head._next = prev; | ||
prev = this._head; | ||
if(next == null) | ||
break | ||
this._head = next; | ||
next = next._next; | ||
} | ||
} | ||
LinkedList.prototype[Symbol.iterator] = function iterator() { | ||
@@ -45,0 +63,0 @@ var curr = undefined; |
@@ -8,3 +8,3 @@ | ||
test('stack: push and pop', () => { | ||
test('stack', () => { | ||
var stack = new LinkedList(); | ||
@@ -19,3 +19,3 @@ stack.pushFirst('hello'); | ||
test('queue: add and remove', () => { | ||
test('queue', () => { | ||
var queue = new LinkedList(); | ||
@@ -30,4 +30,15 @@ queue.pushLast('hello'); | ||
test('reverse stack', () => { | ||
var stack = new LinkedList(); | ||
stack.pushFirst('hello'); | ||
stack.pushFirst('sweet'); | ||
stack.pushFirst('world'); | ||
stack.reverse(); | ||
expect(stack.popFirst()).toBe('hello'); | ||
expect(stack.popFirst()).toBe('sweet'); | ||
expect(stack.popFirst()).toBe('world'); | ||
}); | ||
//Testing iterator | ||
@@ -34,0 +45,0 @@ /* |
5360
145