Quick actions

cmd+k|ctrl+k

Navigation

Languages

Append List

Snippet info

Language

JavaScript

Visibility

public

Author

ryanlove64

Created

2019-06-07T00:14:39Z

Updated

2019-06-20T23:32:50Z


class Node{
    constructor(value){
        this.value = value;
        this.next = null;

    }
    
}

class Ourlist{
  constructor(value){
   this.head = {
         value: value,
         next: null

   }
   this.tail = this.head;
   this.length = 1;

  }


prepend(value){
    //code here
    const newNode = new Node(value);
    
    newNode.next = this.head;
    this.head = newNode;
    this.length++;
    return this;
}

  append(value){
      
      const newNode = new Node(value);
    
     this.tail.next = newNode;
     this.tail = newNode;
     this.length++;
     return this.head;
    
   
  }
  

  PrintTree(temp_head){
    this.next = this.head;
       while(this.next !== null){
         console.log(this.next);
          this.next = this.next.next;
       }
  }
  
  printlist(){
      const  array =[];
        let currentNode = this.head;
        while(currentNode !== null){
            array.push(currentNode.value);
            currentNode = currentNode.next;
        }
        console.log(array);
        return array;
      }

      
  
  
  insert(index,value){
      const  newNode = new Node(value)
         if(index >= this.length){
         return this.append(value);    
         }
       
   }

     TraverseToIndex(index){ // check index for valid one
            let counter = 0;
           let  currentNode = this.head;
              while(counter !== index){
                  console.log(currentNode);
                 currentNode = currentNode.next;
                 counter++;
                 if(currentNode.next == null){
                      return currentNode;
                
                 }
               }
              return currentNode;
            }
      
      
      remove(index){
          const leader = this.TraverseToIndex(index-1);
         const unwantedNode = leader.next;
         leader.next = unwantedNode.next;
         this.lenght--;
         return this.printlist();
      }

}


const LinkedList = new Ourlist(10);
let temp;
LinkedList.append(16);
LinkedList.append(5);
LinkedList.append(22);
LinkedList.append(32);
LinkedList.prepend(1);
LinkedList.insert(2,44);
LinkedList.printlist();
LinkedList.remove(4);


//LinkedList.PrintTree(temp);
//console.log(LinkedList);
INFO