当前位置:网站首页>JS linked list 02

JS linked list 02

2022-07-28 15:56:00 PBitW

get Realization – Gets the element value at the specified location

Code

// 5 get Method 
LinkedList.prototype.get = function(position){
    
  // 1  Cross border judgment  --  Relevant positions should be judged to be out of bounds 
  if(position < 0 || position >= this.length) return null;

  // 2  Get the corresponding data 
  let current = this.head;
  let index = 0;
  while(index++ < position){
    
    current = current.next;
  }
  return current.data;
}

indexOf Realization – Return the specified value position

Code

// 6 indexOf Method 
LinkedList.prototype.indexOf = function(data){
    
  // 1  Defining variables 
  let current = this.head;
  //  Record the return location 
  let index = 0;

  // 2  Start looking for 
  while(current){
    
    if(current.data === data){
    
      return index;
    }
    current = current.next;
    index += 1;
  }

  //  No return found -1
  return -1;
}

update Realization – Modify the specified position value

Code

// 7 update Method 
LinkedList.prototype.update = function(position,newData){
    
  // 1  Judge out of bounds 
  if(position < 0 || position >= this.length) return false;

  // 2  Find the correct node 
  let current = this.head;
  let index = 0;
  while(index++ < position){
    
    current = current.next;
  }

  // 3  take position Upper data modify 
  current.data = newData;
  return true;
}

removeAt Method realization – Delete the specified location element

Video code
 Insert picture description here
The code idea of video is clearer , Because the data of two nodes is saved , Better understanding ! My code is what I wrote and inserted last time , You can save with only one variable , But because of me There is no information about the deleted node , Therefore, the data of the node cannot be returned like the video !

My code

// 8 removeAt Method 
LinkedList.prototype.removeAt = function(position){
    
  if(position < 0 || position >= this.length) return false;
  if(position === 0){
    
    this.head = this.head.next;
  }else{
    
    let current = this.head;
    let index = 0;
    while(index++ < position-1){
    
      current = current.next;
    }
    current.next = current.next.next;
  }
  //  Be careful length
  this.length -= 1;
  return true;
}

remove Realization – Remove the specified value

Code

// 9 remove Method 
LinkedList.prototype.remove = function(data){
    
  //  To obtain position 
  let num = this.indexOf(data);
  //  Delete 
  this.removeAt(num);
}

Here rookies don't write the rest isEmpty and size 了 , After all, there is length, These two are Pediatrics !

summary

After understanding the linked list , It is found that the operation of writing the linked list is actually relatively simple , It's worth noting that current To whom You have to make it clear ( Namely while The loop has to figure out how many times it is executed ), Basically All operations depend on current To complete the move , And you The relationship between the given position and the number of operations Also find out , Like arrays, it is from 0 Start , It feels really like an array !

原网站

版权声明
本文为[PBitW]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207281451250761.html