- 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
class Node {
constructor ( value ){
this.value = value;
}
}
class SingleLinkedList {
constructor(){
this.root = null;
}
add ( node ) {
if( this.root == null ){
this.root = node;
return;
}
var current = this.root;
while ( current.next != null ){
current = current.next;
}
current.next = node ;
}
print (){
var current = this.root;
while (current != null){
console.log( current.value);
current = current.next;
}
}
}
var nodeObj1 = new Node ( 22 );
var nodeObj2 = new Node ( 25 );
var nodeObj3 = new Node ( 21 );
var nodeObj4 = new Node ( 20 );
var sll = new SingleLinkedList();
sll.add ( nodeObj1 )
sll.add ( nodeObj2 )
sll.add ( nodeObj3 )
sll.add ( nodeObj4 )
sll.print();