-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.js
88 lines (75 loc) · 1.96 KB
/
Queue.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
class Queue {
constructor() {
this.first = null;
this.last = null;
this.length = 0;
}
peek() {
if (this.top == null || this.length < 0) {
return console.error("stack empty");
}
return this.first;
}
dequeue() {
if (this.first == null || this.length < 0) {
return console.error("stack empty");
}
if (this.first == this.last || this.length == 1) {
this.last = null
}
this.first = this.first.next;
this.length--;
return this.top;
}
enqueue(val) {
let node = new Node(val)
if (this.first == null) {
this.first = node;
this.last = node;
} else {
this.last.next = node;
this.last = node;
}
this.length++;
return this;
}
print_queue() {
if (this.length < 0) {
return console.error("emtyp qeue");
}
if (this.length == 1 | this.first == this.last) {
return this.first;
}
let first = this.first;
let queue = []
while (first) {
queue.push(first.value);
first = first.next;
}
return queue;
}
size() {
return this.length;
}
isEmpty() {
return this.length < 0;
}
}
class Node {
constructor(val) {
this.value = val;
this.next = null;
}
}
let queue = new Queue()
console.log("this peek", queue.peek())
console.log("this peek", queue.enqueue(90))
console.log("this peek", queue.enqueue(100))
console.log("this peek", queue.enqueue(77))
console.log("this peek", queue.enqueue(66))
console.log("this peek", queue.enqueue(34543))
console.log("this peek", queue.enqueue(1054350))
console.log("this queuee", queue.print_queue())
console.log("this peek", queue.dequeue())
console.log("this queuee", queue.print_queue())
console.log("is this empty", queue.isEmpty())