Dia 50/2019 - Estruturas de dados em JS: Filas e Deques

Desafio #92daysofcode que fiz no final de 2019. Nesse artigo temos o dia 50, mostrando o uso de filas e deques (fila duplamente encadeada) com javascript.

Código

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
class Queue {
constructor() {
this.count = 0;
this.lowestCount = 0;
this.items = {};
}

enqueue(element) {
this.items[this.count] = element;
this.count++;
}

dequeue() {
if (this.isEmpty()) {
return undefined;
}
const result = this.items[this.lowestCount];
delete this.items[this.lowestCount];
this.lowestCount++;
return result;
}

peek() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.lowestCount];
}

isEmpty() {
return this.size() === 0;
}

clear() {
this.items = {};
this.count = 0;
this.lowestCount = 0;
}

size() {
return this.count - this.lowestCount;
}

toString() {
if (this.isEmpty()) {
return '';
}
let objString = `${this.items[this.lowestCount]}`;
for (let i = this.lowestCount + 1; i < this.count; i++) {
objString = `${objString},${this.items[i]}`;
}
return objString;
}
}
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
class Deque {
constructor() {
this.count = 0;
this.lowestCount = 0;
this.items = {};
}

addFront(element) {
if (this.isEmpty()) {
this.addBack(element);
} else if (this.lowestCount > 0) {
this.lowestCount--;
this.items[this.lowestCount] = element;
} else {
for (let i = this.count; i > 0; i--) {
this.items[i] = this.items[i - 1];
}
this.count++;
this.items[0] = element;
}
}

addBack(element) {
this.items[this.count] = element;
this.count++;
}

removeFront() {
if (this.isEmpty()) {
return undefined;
}
const result = this.items[this.lowestCount];
delete this.items[this.lowestCount];
this.lowestCount++;
return result;
}

removeBack() {
if (this.isEmpty()) {
return undefined;
}
this.count--;
const result = this.items[this.count];
delete this.items[this.count];
return result;
}

peekFront() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.lowestCount];
}

peekBack() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.count - 1];
}

isEmpty() {
return this.size() === 0;
}

clear() {
this.items = {};
this.count = 0;
this.lowestCount = 0;
}

size() {
return this.count - this.lowestCount;
}

toString() {
if (this.isEmpty()) {
return '';
}
let objString = `${this.items[this.lowestCount]}`;
for (let i = this.lowestCount + 1; i < this.count; i++) {
objString = `${objString},${this.items[i]}`;
}
return objString;
}
}
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
/*
* Queue
***********************************/
const queue = new Queue();

console.log(queue.isEmpty());
// true

queue.enqueue('Nerd');
queue.enqueue('Hemerson');

console.log(queue.peek());
// Nerd

console.log(queue.toString());
// Nerd,Hemerson

console.log(queue.size());
// 2

console.log(queue.isEmpty());
// false

queue.dequeue();

console.log(queue.toString());
// Hemerson

queue.enqueue('Nerd');

console.log(queue.toString());

console.log(queue.peek());
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
/*
* Deque
***********************************/
const deque = new Deque();

console.log(deque.isEmpty());
// true

deque.addBack('Nerd');
deque.addBack('Hemerson');
deque.addBack('Calistênico');

console.log(deque.toString());
// Nerd,Hemerson,Calistênico

console.log(deque.peekFront());
// Nerd

console.log(deque.peekBack());
// Calistênico

console.log(deque.size());
// 3

console.log(deque.isEmpty());
// false

deque.removeBack();

console.log(deque.toString());
// Nerd,Hemerson

deque.removeFront();

console.log(deque.toString());
// Hemerson

Conclusão

A postagem original pode ser vista no meu Instagram e o código está acessível no meu Github.

Relacionados

Ao fechar este aviso ou continuar navegando no site Nerd Calistênico, você aceita o uso de cookies.

Este site usa cookies para assegurar a melhor experiência para os nossos usuários. Consulte nossa política de privacidade.

Uma nova versão está disponível. Clique aqui para atualizar.