Created
July 27, 2022 19:31
-
-
Save faustoct1/c1dbc69ccfba2e2967ba8356c0bda3d2 to your computer and use it in GitHub Desktop.
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 | |
add = (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 | |
remove = (index) => { | |
if(index>this.tamanho-1 || !this.tamanho) return undefined | |
if(index===0){ | |
this.removeFirst() | |
return | |
} | |
if(index===this.tamanho-1){ | |
this.removeLast() | |
return | |
} | |
let i = 0 | |
let obj = this.obj | |
while(true){ | |
if(i===index){ | |
obj.anterior.proximo = obj.proximo | |
obj.proximo.anterior = obj.anterior | |
this.tamanho-- | |
break | |
} | |
obj = obj.proximo | |
i++ | |
} | |
} | |
insertAtFirst = (valor) => { | |
if(this.calda===null){ | |
this.obj = { | |
anterior: this.obj, | |
proximo: null, | |
valor: valor | |
} | |
this.cabeca = this.calda = this.obj | |
this.tamanho++ | |
return | |
} | |
this.obj = { | |
proximo: this.obj, | |
anterior: null, | |
valor: valor | |
} | |
this.obj.proximo.anterior = this.obj | |
this.cabeca = this.obj | |
this.tamanho++ | |
} | |
insert = (index,valor) => { | |
console.log(index,this.tamanho) | |
if(index>=this.tamanho-1 || !this.tamanho) { | |
this.add(valor) | |
return | |
} | |
if(index===0||index<-1){ | |
this.insertAtFirst(valor) | |
return | |
} | |
let i = 0 | |
let atual = this.obj | |
while(true){ | |
if(i===index){ | |
const obj = { | |
proximo: atual.proximo, | |
anterior: atual, | |
valor: valor | |
} | |
atual.proximo = obj | |
this.tamanho++ | |
break | |
} | |
atual = atual.proximo | |
i++ | |
} | |
} | |
removeFirst = () => { | |
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-- | |
} | |
removeLast = () => { | |
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 | |
} | |
const anterior = this.calda.anterior | |
delete this.calda | |
this.calda = anterior | |
delete this.calda.proximo | |
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 ll = new ListLigada() | |
for(let i=0;i<10;i++) ll.add(i+1) | |
console.log('ADD',ll.toArray()) | |
ll.removeLast() | |
console.log('REMOVE ULTIMO', ll.toArray()) | |
ll.removeFirst() | |
console.log('REMOVE PRIMEIRO', ll.toArray()) | |
ll.remove(1) | |
console.log('REMOVE INDEX 1', ll.toArray()) | |
ll.remove(1) | |
console.log('REMOVE INDEX 1', ll.toArray()) | |
ll.remove(2) | |
console.log('REMOVE INDEX 2', ll.toArray()) | |
ll.remove(2) | |
console.log('REMOVE INDEX 2', ll.toArray()) | |
ll.remove(1) | |
console.log('REMOVE INDEX 1', ll.toArray()) | |
ll.remove(1) | |
console.log('REMOVE INDEX 1', ll.toArray()) | |
ll.remove(1) | |
console.log('REMOVE INDEX 1', ll.toArray()) | |
ll.removeFirst() | |
console.log('REMOVE PRIMEIRO', ll.toArray()) | |
ll.add(1) | |
ll.add(2) | |
ll.add(5) | |
ll.insert(0,0) | |
console.log('ADD NO INDEX 0', ll.toArray()) | |
ll.insert(3,6) | |
console.log('ADD NO INDEX 3', ll.toArray()) | |
ll.insert(2,3) | |
console.log('ADD AT INDEX 2', ll.toArray()) | |
ll.insert(3,4) | |
console.log('ADD AT INDEX 3', ll.toArray()) | |
ll.remove(3) | |
console.log('DELETE NO INDEX 3', ll.toArray()) | |
ll.remove(3) | |
console.log('DELETE NO INDEX 3', ll.toArray()) | |
ll.remove(4) | |
console.log('DELETE NO INDEX 4', ll.toArray()) | |
ll.remove(3) | |
console.log('DELETE NO INDEX 3', ll.toArray()) | |
ll.remove(1) | |
console.log('DELETE NO INDEX 1', ll.toArray()) | |
ll.remove(0) | |
console.log('DELETE NO INDEX 0', ll.toArray()) | |
ll.remove(0) | |
console.log('DELETE NO INDEX 0', ll.toArray()) | |
console.log('obj lista ligada', ll.obj) | |
} | |
(async () => { test() })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment