-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.js
90 lines (82 loc) · 2.32 KB
/
linkedList.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
class LinkedList {
constructor (firstItem) {
this.Start = 1
this.Content = [
{ id: 1, next: 0, data: firstItem }
]
this.getItems()
}
getItems () {
console.log('\n', {
Start: this.Start,
Content: this.Content
})
}
getItemsSorted () {
let sortedContent = []
let findID = this.Content.find(({ id }) => id === this.Start)
let nextIndex = findID.id - 1
this.Content.forEach(() => {
sortedContent.push(this.Content[nextIndex])
nextIndex = this.Content[nextIndex].next - 1
})
console.log('\n', 'Sorted:\n', {
Start: this.Start,
Content: sortedContent
})
}
addBefore (newItem, beforeData = null) {
let newID = this.Content.length + 1
let newObject = null
if (!beforeData) {
newObject = { id: newID, next: 0, data: newItem }
for (let i = 0; i < this.Content.length; i++) {
if (this.Content[i].next === this.Content.length - 1) {
this.Content[i].next = newID
break
}
}
} else {
let afterID = -1
let afterIndex = -1
for (let i = 0; i < this.Content.length; i++) {
if (this.Content[i].data === beforeData) {
afterID = this.Content[i].id
afterIndex = i
break
}
}
if (afterIndex !== -1) {
let isNewFirstItem = (this.Start === afterIndex + 1)
if (isNewFirstItem) {
newObject = { id: newID, next: afterIndex + 1, data: newItem }
this.Start = newID
} else {
let beforeIndex = -1
for (let i = 0; i < this.Content.length; i++) {
if (this.Content[i].next === afterID) {
beforeIndex = i
break
}
}
this.Content[beforeIndex].next = newID
newObject = { id: newID, next: afterID, data: newItem }
}
} else {
console.log(`\n Can't add ${newItem} before ${beforeData}, because ${beforeData} doesn't exists`)
}
}
if (newObject) {
this.Content.push(newObject)
}
this.getItems()
}
}
const list = new LinkedList('Chloe')
list.addBefore('Francis')
list.addBefore('Beatrix', 'Chloe')
list.addBefore('David', 'Francis')
list.addBefore('Edward', 'Francis')
list.addBefore('Abigail', 'Beatrix')
list.addBefore('NoOne', 'NonExisting')
list.getItemsSorted()