Skip to content

Instantly share code, notes, and snippets.

@faustoct1
Created July 30, 2022 21:13
Show Gist options
  • Save faustoct1/543dc3dbea6749f2d48c8833dbe15797 to your computer and use it in GitHub Desktop.
Save faustoct1/543dc3dbea6749f2d48c8833dbe15797 to your computer and use it in GitHub Desktop.
Implementando fila com lista ligada em javascript
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() })()
@faustoct1
Copy link
Author

Veja exemplo completo de implementação de lista ligada nesse gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment