Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd2a685

Browse files
Testing search
1 parent 7e8654f commit fd2a685

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

‎linkedList.js

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,62 @@
11
function LinkedList() {
2-
this.head = null;
3-
this.tail = null;
2+
this.head = null;
3+
this.tail = null;
44
}
55

66
function Node(value, next, prev) {
7-
this.value = value;
8-
this.next = next;
9-
this.prev = prev;
7+
this.value = value;
8+
this.next = next;
9+
this.prev = prev;
1010
}
1111

1212
LinkedList.prototype.addToHead = function(value) {
13-
let newNode = new Node(value, this.head, null);
14-
if (this.head) this.head.prev = newNode;
15-
else this.tail = newNode;
16-
this.head = newNode;
13+
let newNode = new Node(value, this.head, null);
14+
if (this.head) this.head.prev = newNode;
15+
else this.tail = newNode;
16+
this.head = newNode;
1717
};
1818

1919
LinkedList.prototype.addToTail = function(value) {
20-
let newNode = new Node(value, null, this.tail);
21-
if(this.tail) this.tail.next = newNode;
22-
else this.head = newNode;
23-
this.tail = newNode;
20+
let newNode = new Node(value, null, this.tail);
21+
if(this.tail) this.tail.next = newNode;
22+
else this.head = newNode;
23+
this.tail = newNode;
2424
}
2525

2626
LinkedList.prototype.removeHead = function() {
27-
if(!this.head) return null;
28-
let val = this.head.value;
29-
this.head = this.head.next;
30-
if(this.head) this.head.prev = null;
31-
else this.tail = null;
32-
return val;
27+
if(!this.head) return null;
28+
let val = this.head.value;
29+
this.head = this.head.next;
30+
if(this.head) this.head.prev = null;
31+
else this.tail = null;
32+
return val;
3333
}
3434

3535
LinkedList.prototype.removeTail = function() {
36-
if(!this.tail) return null;
37-
let val = this.tail.value;
38-
this.tail = this.tail.prev;
39-
if(this.tail) this.tail.next = null;
40-
else this.head = null;
41-
return val;
36+
if(!this.tail) return null;
37+
let val = this.tail.value;
38+
this.tail = this.tail.prev;
39+
if(this.tail) this.tail.next = null;
40+
else this.head = null;
41+
return val;
4242
}
4343

44-
let ll = new LinkedList();
44+
LinkedList.prototype.search = function(searchValue) {
45+
let currentNode = this.head;
46+
while (currentNode) {
47+
if(currentNode.value === searchValue) return currentNode.value;
48+
currentNode = currentNode.next;
49+
}
50+
return null;
51+
}
52+
53+
let myLL = new LinkedList();
4554

46-
ll.addToHead('one');
47-
ll.addToHead('two');
48-
ll.addToHead('three');
55+
myLL.addToHead(123);
56+
myLL.addToHead(70);
57+
myLL.addToHead('hello');
58+
myLL.addToTail(19);
59+
myLL.addToTail('world');
60+
myLL.addToTail(20);
4961

50-
console.log(ll.removeTail());
62+
console.log(myLL.search(10));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /