-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.js
133 lines (112 loc) · 3.13 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
// List implementation
var List = function() {
// list properties
this.listSize = 0;
this.position = 0;
this.dataStorage = [];
};
// add an element to the List
List.prototype.append = function(element) {
this.dataStorage[this.listSize++] = element;
};
// find() helper function
List.prototype.find = function(element) {
for (var i = 0; i < this.dataStorage.length; i++) {
if (this.dataStorage[i] === element) {
return i;
}
}
return -1;
}
// remove an element form the List
List.prototype.remove = function(element) {
var foundAt = this.find(element); // get index of element from find()
if (foundAt > -1) {
this.dataStorage.splice(foundAt, 1);
this.listSize--; // reduce List size
return true; // confirmation
}
return false;
};
List.prototype.length = function() {
return this.listSize;
};
List.prototype.toString = function() {
return this.dataStorage;
};
List.prototype.insert = function(element, after) {
var insertPos = this.find(after);
if (insertPos > -1) {
this.dataStorage.splice(insertPos + 1, 0, element);
this.listSize++;
return true;
}
return false;
};
List.prototype.clear = function() {
delete this.dataStorage;
this.dataStorage = [];
this.dataStorage.length = 0;
};
List.prototype.contains = function(element) {
for (var i = 0; i < this.dataStorage.length; i++) {
if (element === this.dataStorage[i]) {
return true;
}
}
return false;
};
// List traversals via iterators
/* Why iterators? they allow us to traverse a List without referencing the internal storage mechanism of the List class */
/* Advantages of using iterators
1. not have to worry about the underlying data structure when accessing list items
2. being able to update the list and not having tho update the iterator, where
an index becomes invalid when a new element is added to the list
3. providing a uniform means of accessing the elements for different types of
data stores used in the implementaion of the List class
*/
List.prototype.front = function() {
this.position = 0;
};
List.prototype.end = function() {
this.position = this.listSize - 1;
};
List.prototype.prev = function() {
if (this.position > -1) this.position--;
};
List.prototype.next = function() {
if (this.position < this.listSize) this.position++;
};
List.prototype.currPosition = function() {
//if (this.posititon === 0) return false;
return this.position;
};
List.prototype.moveTo = function(location) {
this.position = location;
};
List.prototype.getElement = function() {
return this.dataStorage[this.position];
};
var names = new List();
names;
names.append('kevin');
names.append('Ray');
names.append('heather');
names.append('chuck');
names.append('Rudy');
names;
names.front(); // iterate
names.getElement(); //kevin
names.next(); // itereate
names.getElement(); // Ray
names.next();
names.next();
names.getElement(); //chuck
traversing a List front to back
for (names.front(); names.currPosition < names.length(); names.next()) {
console.log(names.getElement());
}
// iterate over a List back to front
for (names.end(); names.currPosition() >= 0; names.prev()) {
console.log(names.getElement());
}