-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
118 lines (102 loc) · 2.7 KB
/
main.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
class ConnectedList {
head;
constructor(){
let total_len = arguments.length
this.head = new Elem(arguments[0], null)
let counter = 1
let prev_element = this.head
while (counter < total_len){
let next_element = new Elem(arguments[counter], null)
prev_element.link = next_element
prev_element = next_element
counter += 1
}
}
print(){
let temp_head = this.head
let temp_str = String()
while (temp_head != null){
temp_str = temp_str + `${temp_head.value} `
temp_head = temp_head.link
}
return temp_str
}
append(new_value){
if (this.head == null){
this.head = new Elem(new_value)
}else{
let temp_head = this.head
while (temp_head.link != null){
temp_head = temp_head.link
}
temp_head.link = new Elem(new_value, null)
}
}
length(){
let counter = 0
let temp_head = this.head
while (temp_head != null){
temp_head = temp_head.link
counter += 1
}
return counter
}
//
remove(value_to_remove){
let temp_head = this.head
let prev_element = null
while (temp_head.link != null){
prev_element = temp_head
temp_head = temp_head.link
if (temp_head.value == value_to_remove){
prev_element.link = temp_head.link
}
}
}
clear(){
this.head = null
}
//
insert(index, new_value){
let counter = 0
let temp_head = this.head
let prev_element = null
while (temp_head.link != null){
prev_element = temp_head
temp_head = temp_head.link
counter += 1
if (counter == index){
let new_node = new Elem(new_value)
prev_element.link = new_node
new_node.link = temp_head
break
}
}
}
//
merge(anotherList){
let temp_head = this.head
let prev_element = null
while (temp_head != null){
prev_element = temp_head
temp_head = temp_head.link
}
prev_element.link = anotherList.head
}
searchByValue(value_to_search){
let temp_head = this.head
while (temp_head.link) {
if (temp_head.value == value_to_search){
return temp_head
}
}
}
}
class Elem {
link;
value;
constructor(value, link_next){
this.value = value
this.link = link_next
}
}