forked from learn-co-students/linked-list-lab-g-416
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (86 loc) · 2.78 KB
/
index.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
function getName (node){
return node.name
}
function headNode(list, collection){
let head = collection[list]
return head
}
function next(head, collection) {
return collection[head.next]
}
//nodeAt(0, 'whana', collection)
//let collection = {rkjasj: secondNode, asnan: lastNode, whana: firstNode}
function nodeAt(index, head, collection){
let node = collection[head];
for (let i=0; i < index; i++){
node = next(node, collection)
}
return node
}
//return the key
//0, head, collection
//let collection = {rkjasj: secondNode, asnan: lastNode, whana: firstNode}
//let firstNode = {name: 'susie', next: 'rkjasj'}
//let secondNode = {name: 'sam', next: 'asnan'}
//let lastNode = {name: 'charlie', next: null}
function addressAt(idx, head, collection){
if (idx === 0 ){
return head
} else {
let node = collection[head];
for (let i = 0; i < idx;i++){
node = node.next
}
return node
}
}
//return the index
//traverse the list, find the node, return the index
//indexAt(secondNode, collection, linkedList)).toEqual(1)
//let secondNode = {name: 'sam', next: 'asnan'}
//1
function indexAt(node, collection, head){
let nodeCheck = collection[head]//firstNode
let index = 0;
while (nodeCheck !== node){
nodeCheck = next(nodeCheck, collection)
index++
}
return index;
}
//insertNodeAt(1, 'ajhsak', linkedList, collection)
// let firstNode = {name: 'susie', next: 'rkjasj'}
// let secondNode = {name: 'sam', next: 'asnan'}
// let newNode = {name: 'jill', next: ''}
// let lastNode = {name: 'charlie', next: null}
// let collection = {rkjasj: secondNode,
// asnan: lastNode,
// whana: firstNode,
// ajhsak: newNode}
// let linkedList = 'whana'
function insertNodeAt(index, newHead, head, collection){
// let node = nodeAt(index, head, collection)
// let prev = nodeAt(index - 1, head, collection)
let currentNode = addressAt(index, head, collection) //rkjasj
let prev = nodeAt(index-1, head, collection) //{objectnode}
collection[newHead].next = currentNode
prev.next = newHead
return collection[newHead].next
}
// let firstNode = {name: 'susie', next: 'rkjasj'}
// let secondNode = {name: 'sam', next: 'asnan'}
// let newNode = {name: 'jill', next: ''}
// let lastNode = {name: 'charlie', next: null}
// let collection = {rkjasj: secondNode,
// asnan: lastNode,
// whana: firstNode,
// ajhsak: newNode}
// let linkedList = 'whana'
// deleteNodeAt(1, linkedList, collection)
function deleteNodeAt(index, head, collection){
let current = nodeAt(index, head, collection) //{full ObjNode}
let next = current.next
let prev = nodeAt(index-1, head, collection)
prev.next = next
return prev.next
}