-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queues.js
72 lines (66 loc) · 1.15 KB
/
Queues.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
class NewNode{
constructor(value){
this.value=value
this.next=null
}
}
class Queue{
constructor(){
this.first={
value:null,
next:null
};
this.last=this.first;
this.length=0;
}
enqueue(value){
let newnode=new NewNode(value)
if (this.length===0){
this.first.value=value;
this.length++
return this
}
this.last.next=newnode;
this.last=newnode;
this.length++;
return this;
}
dequeue(){
let currentnode=this.first;
this.first=currentnode.next;
this.length--;
return this
}
printlist(){
let list=[]
let currentnode=this.first
while (currentnode){
list.push(currentnode.value)
currentnode=currentnode.next;
}
return list;
}
peek(){
if (this.length>0){
return this.first.value
}
return "None"
}
isempty(){
if (this.length===0){
return "True"
}
return "False"
}
}
queue=new Queue()
queue.enqueue(100)
queue.enqueue(101)
queue.enqueue(1010)
queue.dequeue()
queue.dequeue()
queue.dequeue()
console.log(queue.isempty())
console.log(queue.peek())
//console.log(queue)
//console.log(queue.printlist())