Repository navigation
Expand file tree
/
Copy pathlinkedList.js
More file actions
105 lines (87 loc) · 2.16 KB
/
Copy pathlinkedList.js
File metadata and controls
105 lines (87 loc) · 2.16 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
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
function linkedList() {
let rootItem = null;
let itemCount = 0;
function add(itemToAdd){
itemToAdd.next = null;
if (rootItem === null){
rootItem = itemToAdd;
itemCount++;
}
else {
if (rootItem.next === null){
rootItem.next = itemToAdd;
itemCount++;
}
else {
//current node already points to next node
//so we need to navigate to that value,
//then recurse until we find end list
addToEnd(rootItem.next, itemToAdd);
}
}
}
function findAllWhereAgedEqualOrAbove(minAge){
//this should be smarter, take a property name, examine each value on the object
let matches = [];
if (rootItem !== null){
if (rootItem.age >= minAge){
matches.push(rootItem.name);
}
if (rootItem.next != null){
findAllNodesEqualOrAbove(rootItem.next, minAge, matches);
return matches;
}
}
}
function findAllNodesEqualOrAbove(node, minAge, matches){
if (node != null) {
if (node.age >= minAge){
matches.push(node.name);
}
}
if (node.next != null){
//console.log(node.name + " : aged " + node.age + " min age" + minAge);
findAllNodesEqualOrAbove(node.next, minAge, matches);
}
return matches;
}
function addToEnd(itemInList, itemToAdd){
console.log(itemInList.name);
if (itemInList.next === null){
itemInList.next = itemToAdd;
itemCount++;
}
else{
add(itemInList.next, itemToAdd);
}
}
function count(){
return itemCount;
}
return {
count: count,
add: add,
findAllWhereAgedEqualOrAbove: findAllWhereAgedEqualOrAbove
};
}
let john = {
name:"john",
age:21
};
let ruby = {
name:"ruby",
age:48
};
let phil = {
name:"phil",
age:35
}
let store = linkedList();
store.add(john);
store.add(ruby);
store.add(phil);
console.log("count : " + store.count());
let results = store.findAllWhereAgedEqualOrAbove(20);
for (let result of results){
console.log("Result : " + result);
}