数据结构day2-1 链表结构 单向链表

 

 

 

 

一、链表封装

 

  // 封装链表类
  function LinkedList() {
    //内部类 节点类
    function Node(data) {
      this.data = data
      this.next = null
    }
    this.head = null
    this.length = 0
    LinkedList.prototype.append = function(data) {
      var newNode = new Node(data)
      //判断是否添加的为第一个节点 length判断
      if (this.length == 0) {
        // var newNode = new Node(data)
        this.head = newNode;
      } else {  //如果不是第一个节点 则找到最后一个节点 然后让最后一个节点的next等于(指向)新节点
        // var newNode = new Node(data)

        // current 一直找 next为空的话 就是最后一个节点
        var current = this.head
        while(current.next) {
          current = current.next
        }
        current.next = newNode
      }
      this.length += 1
    }
    LinkedList.prototype.toString = function() {
      // 定义变量
      var current = this.head;
      var listString = ''
      while(current) {
        listString += current.data + ' '
        current = current.next
      }
      return listString;
    }
    LinkedList.prototype.insert = function(position,data) {
      // 对position进行越界判断  只有4个元素 如果position 为100 也不行
      if (position < 0 || position > list.length) return false
      // 根据data创建newNode
      var newNode = new Node(data)
      if (position == 0) {
        // 先把原来的第一个(this.head指向的正好是原来第一个)赋值给newNode 再把this.head指向newNode
        newNode.next = this.head
        this.head = newNode
      } else {
        //position > 1
        var index = 0
        var current = this.head //while循环让current的next一直指向下一个元素
        var previous = null
        while(index ++ < position) {  // ++index 先加在判断 index++ 先判断在++ 
          previous = current
          current = current.next
        }
        newNode.next = current
        previous.next = newNode
      }
      this.length += 1
      return true
    }
    LinkedList.prototype.get = function(position) {
      // 对position进行越界判断 如果等于length的话则超出了
      if (position < 0 || position >= list.length) return null
      // 获取对应的data
      var index = 0
      var current = this.head 
      // position = 2
      // index =0 current=
      // index =1 current=
      // index =2 跳出循环
      while(index++ < position) {
        current = current.next
        // index ++  //跟上面写法一样 while(index++ < position)
      }

      return current.data
    }
    //indexOf 通过data获取下标
    LinkedList.prototype.indexOf = function(data) {
      var current = this.head
      var index = 0
      while(current) {
        if(current.data == data) {
          return index
        }
        current = current.next
        index += 1
      }
      // 找到最后没有找到 返回-1
      return -1
    }
    LinkedList.prototype.updata = function(position,newData) {
      if (position < 0 || position >= this.length) return false
      var current = this.head
      var index = 0
      while (index ++ < position) {
        current = current.next
      }
      // 将position未知的node的data修改为newData
      current.data = newData
      return true
    }

    LinkedList.prototype.removeAt = function(position) {
      if (position < 0 || position >= this.length) return null
      var current = this.head
      if (position == 0) {
        this.head = this.head.next
      } else {
        var index = 0
        // var current = this.head  返回被删除的元素 不返回true false 所以移到外部
        var previous = null
        while (index ++ < position) {
          previous = current
          current = current.next
        }
        previous.next = current.next
      }
      this.length -= 1
      return current.data //返回被删除的元素
    } 

    LinkedList.prototype.remove = function(data) {
      // 1.获取data在列表中的位置

      var position = this.indexOf(data)

      // 2. 根据位置信息删除节点
      return this.removeAt(position)
    }

    LinkedList.prototype.isEmpty = function() {
      return this.length == 0
    }
    LinkedList.prototype.size = function() {
      return this.length
    }
  }

  var list = new LinkedList()
  list.append('abc')
  list.append('cba')
  list.append('nba')
  alert(list)
  list.insert(0,'aaa')
  list.insert(3,'bbb')
  list.insert(5,'ccc')
  alert(list)
  alert(list.get(0))
  alert(list.get(3))
  alert(list.get(5))

  alert(list.indexOf('abc'))
  alert(list.indexOf('bbb'))
  alert(list.indexOf('ccc'))

  //测试update
  list.updata(0,'mmm')
  list.updata(3,'nnn')
  alert(list)

  list.removeAt(0)
  alert(list)
  list.removeAt(3)
  alert(list)

  alert('-----------')
  //remove
  list.remove('nnn')
  alert(list)
  list.remove('abc')
  alert(list)

  alert(list.isEmpty())
  alert(list.size())

 

posted @ 2019-07-10 16:01  suanmei  阅读(123)  评论(0编辑  收藏  举报