-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-concat-solution.js
136 lines (120 loc) · 3.33 KB
/
09-concat-solution.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
class List {
constructor() {
this.data = new Array(10)
this.size = 0
}
// a simple method that returns the value at the specified index
// if the index is within the valid range of elements in the list.
// returns null if someone tries to access an index out of range
// of the list.
get(index) {
if (index < this.size) {
return this.data[index]
}
return null
}
// overwrites the value at the specified index,
// and the index must be within a valid range of the current size of the list.
set(index, value) {
if (index >= 0 && index < this.size) {
this.data[index] = value
}
}
// add the value to the first-most free position
// in the data array
push(value) {
if (this.size === this.data.length) {
this.grow()
}
this.data[this.size] = value
this.size++
}
// remove(index=1)
// aa = [5, 8, 12, 13, 19]
// removed = 8
remove(removeIndex) {
if (this.size === 0) {
return null;
}
// save the value that was at the index
let removed = this.data[removeIndex]
// scoot all values after over left by one
for (let i = removeIndex; i < this.size - 1; i++) {
this.data[i] = this.data[i + 1]
}
// manually overwrite the now-stale data
this.data[this.size - 1] = null
// decrement the size to show one item was removed
this.size--
// return the value of the removed item
return removed
}
grow() {
// create a new array that's twice as big
let aa = new Array(this.data.length * 2)
// copy over every value from the old data array to the larger array
for (let i = 0; i < this.data.length; i++) {
aa[i] = this.data[i]
}
// set the larger area as the data for the list
this.data = aa
}
add(index, value) {
if (this.size === this.data.length) {
this.grow()
}
for (let i = this.size; i > index; i--) {
this.data[i] = this.data[i - 1]
}
this.data[index] = value
this.size++
}
contains(value) {
for (let i = 0; i < this.size; i++) {
if (this.data[i] === value) {
return true;
}
}
return false;
}
concat(other) {
let result = new List()
for (let i = 0; i < this.size; i++) {
result.push(this.get(i))
}
for (let i = 0; i < other.size; i++) {
result.push(other.get(i))
}
return result
}
toString() {
if (this.size === 0) {
return "[]"
} else {
let result = ""
for (let i = 0; i < this.size; i++) {
result += this.data[i] + " "
}
return "[" + result + "]"
}
}
}
const ll = new List()
ll.push(5)
ll.push(8)
ll.push(12)
ll.push(13)
ll.push(19)
const l2 = new List()
ll.push(23)
ll.push(24)
ll.push(25)
const l3 = ll.concat(l2)
console.log(l3.get(0) === 5)
console.log(l3.get(1) === 8)
console.log(l3.get(2) === 12)
console.log(l3.get(3) === 13)
console.log(l3.get(4) === 19)
console.log(l3.get(5) === 23)
console.log(l3.get(6) === 24)
console.log(l3.get(7) === 25)