-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.js
162 lines (138 loc) · 3.55 KB
/
List.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Creates a new List. A list lets you add and remove elements at the current position
* of the iterator, which starts at the start of the list and can be moved with the functions.
*/
var List = (function () {
"use strict";
function List() {
this.head = null;
this.tail = null;
this.previows = null;
this.following = null;
this.length = 0;
}
List.prototype = {
// Adds the element between the previows and following (private method)
add: function (item) {
var node = {
data: item,
next: this.following,
prev: this.previows
};
if (this.head === null) {
this.head = node;
this.tail = node;
} else {
if (this.previows) {
this.previows.next = node;
} else {
this.head = node;
}
if (this.following) {
this.following.prev = node;
} else {
this.tail = node;
}
}
this.length += 1;
return node;
},
// Adds the item and then moves the iterator to the following position
addNext: function (item) {
var node = this.add(item);
this.following = node.next;
this.previows = node;
},
// Adds the item and then moves the iterator to the previows position
addPrev: function (item) {
var node = this.add(item);
this.following = node;
this.previows = node.prev;
},
// Removes the follwing element and sets the next one as the new following element
removeNext: function () {
// Cant remove next if there isnt one
if (this.following === null) {
return;
}
if (this.following.prev) {
this.following.prev.next = this.following.next;
}
this.following = this.following.next;
this.length -= 1;
},
// Removes the previows element and sets the prev one as the new previows element
removePrev: function () {
// Cant remove prev if there isnt one
if (this.previews === null) {
return;
}
if (this.previows.prev) {
this.previows.prev.next = this.previows.next;
}
this.previows = this.previows.prev;
this.length -= 1;
},
// Returns the first elements data
first: function () {
return this.head.data;
},
// Returns the last elements data
last: function () {
return this.tail.data;
},
// Returns true if the queue is empty, and false otherwise
isEmpty: function () {
return this.head === null;
},
// Returns the size of the list
size: function () {
return this.length;
},
// Sets the current on the first element
iterate: function () {
this.following = this.head;
this.previows = null;
},
// Sets the current on the last element
iterateLast: function () {
this.following = null;
this.previows = this.tail;
},
// Moves to the next element if there is one
next: function () {
if (this.hasNext()) {
this.following = this.following.next;
this.previows = this.following;
}
},
// Moves to the previews element if there is one
prev: function () {
if (this.hasPrev()) {
this.following = this.previows;
this.previows = this.previows.prev;
}
},
// Checks if there is a next elements (from the current one)
hasNext: function () {
return this.following !== null;
},
// Checks if there is a previews element (from the current one)
hasPrev: function () {
return this.previows !== null;
},
// Returns the following elements data
getNext: function () {
if (this.hasNext()) {
return this.following.data;
}
},
// Returns the previws elements data
getPrev: function () {
if (this.hasPrev()) {
return this.previows.data;
}
}
};
return List;
}());