Skip to content

Commit 326c72c

Browse files
committed
Add doublyLinkedLists.js
1 parent 96a8d91 commit 326c72c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Chapter6/doublyLinkedLists.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function Node(element) {
2+
this.element = element;
3+
this.previous = null;
4+
this.next = null;
5+
}
6+
7+
function LList() {
8+
this.head = new Node("head");
9+
this.find = find;
10+
this.insert = insert;
11+
this.display = display;
12+
}
13+
14+
function find(item) {
15+
var currNode = this.head;
16+
while(currNode.element != item) {
17+
currNode = currNode.next;
18+
}
19+
return currNode;
20+
}
21+
22+
function insert(newElement, item) {
23+
var newNode = new Node(newElement);
24+
var current = this.find(item);
25+
newNode.next = current.next;
26+
newNode.previous = current;
27+
current.next = newNode;
28+
}
29+
30+
function display() {
31+
var currNode = this.head;
32+
while(currNode.next != null) {
33+
console.log("Current:", currNode.next.element, " previous:", currNode.next.previous, " next:", currNode.next.next);
34+
currNode = currNode.next;
35+
}
36+
}
37+

Chapter6/test.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>test</title>
6+
</head>
7+
<body>
8+
<script src="doublyLinkedLists.js"></script>
9+
<script>
10+
var list = new LList();
11+
list.insert("First", "head");
12+
list.insert("Second", "First");
13+
list.insert("Third", "Second");
14+
list.display();
15+
</script>
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)