링크 데이터 구조 js 구현

4191 단어
최근 에 js 데이터 구조 와 알고리즘 을 배 우 는 책 을 읽 고 책의 예 와 방법 에 따라 이 루어 졌 다.기록 은 다음 과 같 습 니 다.



    
      




  function LinkedList(){
    //      
    let Node = function(element){
      this.element = element
      this.next = null
    }
    let length = 0
    let head = null

    //       
    this.append = function(element){
      //                
      //        
      let node = new Node(element),
      current
      //           ,       ,         
      //          
      if(head == null){
        //  head           
        head = node
      }else{
        //            
          current = head
        while(current.next){
          current = current.next
        }
        current.next = node
      }
      length ++
    }
    //         
    this.removeAt = function(position){
      //      
      if(position > -1 && position < length){
        let current = head,
        previous,
        index = 0;
        //      
        if(position === 0){
          head = current.next
        }else{
          while(index++ <  postion){
            previous = current
            current = current.next
          }
          //  previous current        ,  current,     
          previous.next = current.next
        }
        length -- 
        return current.element
      }else{
        return null 
      }
    }
    //     
    this.resize = function(){
      return length
    }

    //          
    this.insert = function(position, element){
      //      
      if(position >=0 && position <= length){
        //               
        let node = new Node(element),
        current = head,
        previous,
        index = 0
        //        
        if(position === 0){
          node.next = current
          head = node
        }else{
          //          
          while(index++ <= position){
            previous = current
            current = current.next
          }
          //            
          node.next = current
          previous.next = node
        }
        length ++
        return true
      }else{
        return false
      }
    }
    // toString()  
    this.toString = function(){
      let current = head,
      string = ''
      while(current){
        string += current.element + (current.next ? 'n' : '')
        current = current.next
      }
      return string
    }
    // indexOf()
    this.indexOf = function(element){
      let current = head
      index = 0
      while(current){
        if(element === current.element){
          return index
        }
        index ++
        current = current.next
      }
      return -1
    }
    //   
    this.remove = function(element){
      let index = this.indexOf(element)
      return this.removeAt(index)
    }
    // isEmpty
    this.isEmpty = function(){
      //               true     false
      return length === 0
    }
    //     
    this.getHead = function(){
      return head
    }
  }

  //     
  function DoublyLinkedList() {
    let Node = function(element){
      this.element = element
      this.next = null 
      this.prev = null
    }
    let length = 0
    let head = null
    let tail = null
    //           
    this.insert = function(position, element){
      //      
      if(position >=0 && position <= length){
        let node = new Node(element),
        current = head,
        previous,
        index = 0
        if(position === 0){
          if(!head){
            head = node
            tail = node
          }else{
            node.next = current
            current.prev = node
            head = node
          }
        }else if(position === length){
          current = tail
          current.next = current
          tail = node
        }else{
          while(index++ < position){
            previous = current
            current = current.next
          }
          node.next = current
          previous.next = node
          current.prev = node
          node.prev = previous
        }
        length ++
        return true
      }else{
        return false
      }
    }
  }




좋은 웹페이지 즐겨찾기