-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinked_list.js
More file actions
117 lines (97 loc) · 2.26 KB
/
linked_list.js
File metadata and controls
117 lines (97 loc) · 2.26 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
/*
Implement Linked List
*/
function Node(data, link) {
this.data = data || 0,
this.link = link || null;
}
function LinkedList(node) {
this.head = node || null;
this.count = this.head === null ? 0 : 1;
}
// Traverse the list and add at the end
LinkedList.prototype.append = function(node) {
if (this.head === null) {
this.head = node;
this.count++;
return;
}
var temp = this.head;
while (temp.link !== null) {
temp = temp.link;
}
temp.link = node;
node.link = null;
this.count++;
}
LinkedList.prototype.deleteElement = function(node) {
console.log("Deleting the node : " + node.data);
if (this.head === node) {
var new_head = this.head.link;
delete this.head['data'];
delete this.head['link'];
this.head = new_head;
this.count--;
return;
}
var curr = this.head;
while (curr.link != null) {
if (curr.link == node) {
var temp_node = curr.link;
curr.link = temp_node.link
delete temp_node.data;
delete temp_node.link;
this.count--;
break;
}
}
}
LinkedList.prototype.display = function() {
console.log("Here're the list of nodes : ")
var temp = this.head,
list_items = [];
while (temp != null) {
list_items.push(temp.data);
temp = temp.link;
}
console.log(list_items);
}
LinkedList.prototype.reverse = function() {
console.log("Reversing the linked list");
// a -> b -> c
var prev = null,
curr = this.head,
next = curr.link;
while (next !== null) {
next = curr.link;
curr.link = prev;
prev = curr;
curr = next;
}
if (prev !== null) this.head = prev;
}
LinkedList.prototype.addatbeg = function() {
};
LinkedList.prototype.addafter = function() {
};
/* Build List. Append. Reverse */
var first_node = new Node(1),
second_node = new Node(2),
third_node = new Node(3);
var list = new LinkedList();
list.append(first_node);
list.append(second_node);
list.append(third_node);
list.display();
list.reverse();
list.display();
console.log("List count " + list.count);
/* Test reverse list with one node. */
var list_with_one_node = new LinkedList(new Node(1));
list_with_one_node.display();
list_with_one_node.reverse();
list_with_one_node.display();
list.display();
list.deleteElement(second_node);
list.display();
console.log("List count " + list.count);