Repository navigation
Expand file tree
/
Copy pathexample.js
More file actions
71 lines (62 loc) · 1.62 KB
/
Copy pathexample.js
File metadata and controls
71 lines (62 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
function Node(value, next, prev) {
this.value = value;
this.next = next || this;
this.prev = prev || this;
}
function LinkedList() {
this._front = null;
}
LinkedList.prototype.push = function LinkedList_push(value) {
if (this._front === null) {
this._front = new Node(value);
} else {
var back = this._front.prev;
var n = new Node(value, this._front, back);
back.next = n;
this._front.prev = n;
}
};
LinkedList.prototype.unshift = function LinkedList_unshift(value) {
this.push(value);
this._front = this._front.prev;
};
LinkedList.prototype.pop = function LinkedList_pop() {
if (this._front === null) {return undefined};
this._front = this._front.prev;
return this.shift();
};
LinkedList.prototype.shift = function LinkedList_shift() {
var value = this._front.value;
var front = this._front.next;
var back = this._front.prev;
if (front === this._front) {
this._front = null;
} else {
front.prev = back;
back.next = front;
this._front = front;
}
return value;
};
LinkedList.prototype.count = function() {
if (this._front === null) {
return 0;
} else if (this._front.next === this._front) {
return 1;
} else {
this._front.next = this._front.next.next;
return this.count() + 1;
}
};
LinkedList.prototype.delete = function(match) {
if (this._front.next === this._front && this._front.value === match) {
this._front = null;
} else if (this._front.next.value === match) {
this._front.next = this._front.next.next;
} else {
this._front = this._front.next;
return this.delete(match);
}
};
module.exports = LinkedList;