-
Notifications
You must be signed in to change notification settings - Fork 1
/
createQueueLL.js
81 lines (81 loc) · 2.76 KB
/
createQueueLL.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
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
};
var _length, _head, _tail;
class LinkedList {
constructor() {
_length.set(this, 0);
_head.set(this, void 0);
_tail.set(this, void 0);
}
addLast(item) {
const node = {
value: item,
next: null
};
if (__classPrivateFieldGet(this, _length) === 0) {
__classPrivateFieldSet(this, _head, node);
__classPrivateFieldSet(this, _tail, node);
}
else {
__classPrivateFieldGet(this, _tail).next = node;
__classPrivateFieldSet(this, _tail, node);
}
__classPrivateFieldSet(this, _length, +__classPrivateFieldGet(this, _length) + 1);
return node;
}
removeFirst() {
if (__classPrivateFieldGet(this, _length) === 0) {
console.warn("The structure is empty");
return null;
}
const value = __classPrivateFieldGet(this, _head).value;
__classPrivateFieldSet(this, _head, __classPrivateFieldGet(this, _head).next);
__classPrivateFieldSet(this, _length, +__classPrivateFieldGet(this, _length) - 1);
return value;
}
getFirst() {
if (__classPrivateFieldGet(this, _length) === 0) {
console.warn("The structure is empty");
return null;
}
return __classPrivateFieldGet(this, _head).value;
}
size() {
return __classPrivateFieldGet(this, _length);
}
}
_length = new WeakMap(), _head = new WeakMap(), _tail = new WeakMap();
class QueueLL extends LinkedList {
constructor() {
super();
this.enqueue = this.addLast;
this.dequeue = this.removeFirst;
this.peek = this.getFirst;
}
get sizeQueue() {
return super.size();
}
}
const queue = new QueueLL();
console.log("enqueue value 5", queue.enqueue(5));
console.log("enqueue value 12", queue.enqueue(12));
console.log("dequeue:", queue.dequeue());
console.log("enqueue value 7", queue.enqueue(7));
console.log("get the head:", queue.peek());
console.log("dequeue:", queue.dequeue());
console.log("get the head:", queue.peek());
console.log("dequeue:", queue.dequeue());
console.log("get the head:", queue.peek());
console.log("Queue size: ", queue.sizeQueue);
export { QueueLL };