Created
July 30, 2022 21:13
-
-
Save faustoct1/543dc3dbea6749f2d48c8833dbe15797 to your computer and use it in GitHub Desktop.
Implementando fila com lista ligada em javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListLigada{ | |
obj = { | |
anterior: null, | |
proximo: null, | |
valor: null | |
} | |
calda = null | |
cabeca = null | |
tamanho = 0 | |
enfileira = (valor) => { | |
if(this.calda===null){ | |
this.obj = { | |
anterior: this.obj, | |
proximo: null, | |
valor: valor | |
} | |
this.cabeca = this.calda = this.obj | |
this.tamanho++ | |
return | |
} | |
this.calda.proximo = { | |
anterior: this.calda, | |
proximo: null, | |
valor: valor | |
} | |
this.calda = this.calda.proximo | |
this.tamanho++ | |
} | |
length = () => this.tamanho | |
desenfileira = () => { | |
if(!this.tamanho) return undefined | |
if(this.tamanho===1){ | |
this.obj = { | |
anterior: null, | |
proximo: null, | |
valor: null | |
} | |
this.calda = null | |
this.cabeca = null | |
this.tamanho-- | |
return | |
} | |
this.obj = this.obj.proximo | |
this.obj.anterior = null | |
this.cabeca = this.obj | |
this.tamanho-- | |
} | |
toArray = () => { | |
if(!this.tamanho) return [] | |
const array = [] | |
let obj = this.obj | |
do{ | |
array.push(obj.valor) | |
obj = obj.proximo | |
}while(obj!=null) | |
return array | |
} | |
} | |
const test = async () => { | |
const fila = new ListLigada() | |
for(let i=0;i<5;i++) { | |
console.log('BEM VINDO AO POUPA TEMPO','PEGUE SUA SENHA E AGUARDE NA FILA') | |
console.log(`Entrando na fila pessoa ${i}\n\n`) | |
fila.enfileira(i) | |
} | |
for(let i=0;i<5;i++) { | |
console.log(`Aguardando na fila`,fila.toArray()) | |
console.log(`Atendendo pessoa ${i}`) | |
fila.desenfileira() | |
} | |
} | |
(async () => { test() })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Veja exemplo completo de implementação de lista ligada nesse gist