-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
57 lines (45 loc) · 1.04 KB
/
Copy pathqueue.js
File metadata and controls
57 lines (45 loc) · 1.04 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
// estrutura de fila
class Queue {
constructor() {
this.items = [];
}
// add um novo elemento no fim da fila
add(element) {
return this.items.push(element);
}
// remove o primeiro elemento da fila
remove() {
if(this.items.length > 0) {
return this.items.shift();
}
}
// exibe o primeiro elemento da fila
firstElement() {
return this.items[0];
}
// verifica se a fila está vazia
isEmpty(){
return this.items.length == 0;
}
// retorna o tamanho da fila
size(){
return this.items.length;
}
// esvazia a fila
clear(){
this.items = [];
}
}
let stack = new Queue();
stack.add(1);
stack.add(2);
stack.add(4);
stack.add(8);
console.log(stack.items);
stack.remove();
console.log(stack.items);
console.log(stack.firstElement());
console.log(stack.isEmpty());
console.log(stack.size());
stack.clear();
console.log(stack.items);