This repository was archived by the owner on Aug 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedList.js
151 lines (134 loc) · 3.05 KB
/
SinglyLinkedList.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class SinglyLinkedList {
constructor() {
this._front = null;
this._size = 0;
}
// O(1)
_addFront(data) {
let newNode = new SinglyLinkedList._Node(data);
if (this.isEmpty()) {
this._front = newNode;
} else {
newNode.setNext(this._front);
this._front = newNode;
}
++this._size;
}
// O(1)
_getFront() {
if (this.isEmpty()) throw new Error("List is empty");
else {
return this._front.getData();
}
}
// O(1)
_removeFront() {
if (this.isEmpty()) {
throw new Error("invalid remove");
} else if (this._size === 1) {
let ret = this._front;
this._front = null;
--this._size;
return ret;
} else {
let ret = this._front;
this._front = this._front.getNext();
--this._size;
return ret;
}
}
// O(n)
get(index) {
if (index < 0 || index >= this._size) {
throw new Error("invalid index");
} else if (index === 0) {
return this._getFront();
} else {
let current = this._front;
for (let i = 0; i < index; ++i) {
current = current.getNext();
}
return current.getData();
}
}
//O(n)
remove(index) {
if (index < 0 || index > this._size) {
throw new Error("invalid remove operation");
} else if (index === 0) {
return this._removeFront();
} else {
let current = this._front;
for (let i = 0; i < index - 1; ++i) {
current = current.getNext();
}
let ret = current.getNext().getData();
current.setNext(current.getNext().getNext());
--this._size;
return ret;
}
}
// O(n)
add(data, index) {
if (index < 0 || index > this._size) {
throw new Error("invalid add operation");
} else if (index === 0) {
this._addFront(data);
} else {
let current = this._front;
for (let i = 0; i < index - 1; ++i) {
current = current.getNext();
}
let newNode = new SinglyLinkedList._Node(data, current.getNext());
current.setNext(newNode);
++this._size;
}
}
size() {
return this._size;
}
isEmpty() {
return this._size === 0;
}
print() {
let current = this._front;
for (let i = 0; i < this._size; ++i) {
console.log(current.getData());
current = current.getNext();
}
console.log("********************************************");
}
}
SinglyLinkedList._Node = class {
constructor(data, next = null) {
this._data = data;
this._next = next;
}
setNext(next) {
this._next = next;
}
setData(data) {
this._data = data;
}
getNext() {
return this._next;
}
getData() {
return this._data;
}
};
module.exports = SinglyLinkedList;
// main
// let linkedList = new SinglyLinkedList();
// linkedList.add(5, 0);
// linkedList.add(6, 1);
// linkedList.add(16, 1);
// linkedList.add(10, 3);
// linkedList.add(14, 4);
// linkedList.print();
// linkedList.remove(0);
// linkedList.print();
// linkedList.remove(linkedList.size() - 1);
// linkedList.print();
// linkedList.add(500, 2);
// linkedList.print();