-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListsBasicOperations.html
More file actions
139 lines (108 loc) · 3.1 KB
/
LinkedListsBasicOperations.html
File metadata and controls
139 lines (108 loc) · 3.1 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Basic Linked List Operations</title>
<meta charset="UTF-8">
</head>
<body>
<h2>
Basic Operations on a Singly-Linked Lists
</h2>
<p>
A Linked List is a collection of Nodes which are <em>linked</em> to each other by references or pointers.
</p>
<p>
A singly-linked list contains Nodes which have a reference or pointer to the Node that follows and some data.
</p>
<p>
Please open the browser's console to see the output.
</p>
<ul id="output"></ul>
<script type="text/javascript">
//Node Constructor
function Node(next, data) {
// Pointer to the next Node.
this.next = next;
// Data
this.data = data;
}
// Head of the List.
var head = null;
function insertInFront(head, data) {
var newNode = new Node(null, data);
// The new Node points to the head of the list.
newNode.next = head;
// newNode becomes the head of the list and is returned.
return newNode;
}
function find(head, data) {
while (head != null && head.data != data) {
head = head.next;
}
if(head == null){
throw "Could not find the Node containing the given data.";
}
else {
return head;
}
}
function deleteNode(head, data){
var current = head;
// Delete head if it contains the given data.
if (data == head.data) {
this.head = current.next;
return true;
}
while (current) {
if(current.next.data == data){
current.next = current.next.next;
return true;
}
current = current.next;
}
return false;
}
var first = new Node(null, 20);
var second = new Node(null, 30);
var third = new Node(null, 40);
var fourth = new Node(null, 50);
first.next = second;
second.next = third;
third.next = fourth;
head = first;
//Tests
console.log(head);
head = insertInFront(head, 10);
console.log(head);
console.log(head.next);
console.log(deleteNode(head, 50));
//The Node referenced by the variable "third" becomes the last node.
// So third.next should be null.
console.log(third.next);
console.log(deleteNode(head, 10));
console.log(head);
try {
console.log(find(head, 30));
}
catch (e) {
console.log(e);
}
try {
console.log(find(head, 2013));
}
catch (e) {
console.log(e);
}
//Output
//({next:{next:{next:null, data:40}, data:30}, data:20})
//({next:{next:{next:{next:null, data:40}, data:30}, data:20}, data:10})
//({next:{next:{next:null, data:40}, data:30}, data:20})
//true
//null
//true
//({next:{next:{next:null, data:40}, data:30}, data:20})
//({next:{next:null, data:40}, data:30})
//Could not find the Node containing the given data.
</script>
</body>
</html>