Created
March 5, 2020 03:02
-
-
Save jimmont/22424c3b288d10b829913eec3e91be81 to your computer and use it in GitHub Desktop.
LL
This file contains 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
/* | |
add,remove,length, reverse | |
{value,next} | |
*/ | |
class List{ | |
constructor(){ | |
} | |
} | |
class Node{ | |
constructor(name, next=null){ | |
this.name = String(name); | |
this.next = next; | |
} | |
} | |
class LL{ | |
constructor(){ | |
this.head = null; | |
} | |
add(name=''){ | |
let node = this.head; | |
while(node && node.next){ | |
node = node.next; | |
}; | |
if(!node){ | |
this.head = new Node(name); | |
}else{ | |
node.next = new Node(name); | |
} | |
} | |
remove(name=''){ | |
let node = this.head; | |
let parent; | |
while(node){ | |
if(node.name === name){ | |
} | |
node = | |
}; | |
} | |
reverse(){ | |
} | |
get length(){ | |
} | |
toArray(){ | |
const list = []; | |
let next = this; | |
do{ | |
list.push(next); | |
next = next.next; | |
}while(next); | |
return list; | |
} | |
toString(){ | |
return this.name; | |
//return `name: ${ this.name }, next: ${ this.next }`; | |
} | |
string(joiner=', '){ | |
const list = this.toArray(); | |
return list.join(joiner); | |
} | |
print(joiner){ | |
return console.log(this.string(joiner)); | |
} | |
} | |
var list = []; | |
console.log(list.print('')); | |
console.log(list.length); | |
console.log(list.reverse()); | |
console.log(list.remove('I')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment