-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.js
101 lines (96 loc) · 1.51 KB
/
linked_list.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
class Node
{
constructor(value)
{
this.value= value;
}
setNext(next)
{
this.next = next;
}
getNext()
{
return this.next;
}
}
class LinkedList
{
constructor()
{
this.start = null;
}
passThr()
{
if(this.start)
{
let current = this.start;
while(true)
{
console.log(current.value);
if(current.getNext())
{
current = current.getNext();
continue;
}
break;
}
}
}
addItem(value)
{
if(this.start)
{
let current = this.start;
while(true)
{
if(current.getNext())
{
current = current.getNext();
continue;
}
break;
}
current.setNext(new Node(value));
}
else
{
this.start = new Node(value);
}
}
}
let ll = new LinkedList();
ll.addItem(1);
ll.addItem(2);
ll.addItem(3);
ll.addItem(4);
ll.passThr();
console.log('-----------');
ll.addItem(5);
ll.passThr();
console.log('-----------');
function BinarySearch(ar, value)
{
let len = Math.floor(ar.length / 2);
console.log(len);
let copy = [...ar];
if(copy[len] === value)
{
return len;
}
if(value > copy[len])
{
//se genera subarray de la mitad para la derecha
copy = copy.slice(value);
len = Math.floor(copy.length / 2);
console.log(len);
}
else if(value < copy[len])
{
//se genera subarray de la mitad para la izquierda
copy = copy.slice(0, value - 1);
len = Math.floor(copy.length / 2);
console.log(len);
}
console.log(copy);
}
console.log(BinarySearch([1, 2, 3, 4, 5], 2));