zoukankan      html  css  js  c++  java
  • 链表操作

    链表结构

    function linkedList(){
          function Node(data){
            this.data=data
            this.next=null
          }
          this.length=0
          this.head=null
    }

    链表添加元素方法

    linkedList.prototype.append=function(data){
            var newNode =new Node(data)
            if(this.length==0){
              this.head=newNode
            }else{
              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 mess = " "
            while(current){           
                mess+=current.data + ' '
                current=current.next
        }
            return mess
    }

    在链表的指定位置插入元素

    linkedList.prototype.insert = function (index, data) {
            if (index < 0 || index > this.length) return false //判断如果index小于或大于长度就返回false
            var newNode = new Node(data)
            if (index == 0) {
              newNode.next = this.head
              this.head = newNode
            } else {
              var i = 0;
              var current = this.head;
              var pre = null;
              while (i++ < index) {
                pre=current
                current=current.next
              }
              pre.next=newNode
              newNode.next=current
            }
            this.length+=1
            return true
          }

    获取指定位置的元素

    linkedList.prototype.get=function(index){
            if(index<0||index>this.length)return null        
            var current=this.head
            var i=0
            while(i++<index){
              current=current.next
            }
            return current.data
          }

    获取指定元素的位置

    linkedList.prototype.indexOf=function(data){
            var current=this.head
            var i=0
            while(i<this.length){
              if(current.data==data){
                return i
              }
              current=current.next
              i+=1
            }
            return -1
          }

    修改指定位置的元素

    linkedList.prototype.updata=function(index,data){
            if(index<0||index>this.length)return false
            var i=0
            var current=this.head
            while(i++<index){
              current=current.next
            }
            current.data=data
         return true }

    删除指定位置的元素

    linkedList.prototype.delete = function (index) {
            if (index < 0 || index > this.length) return false
            if (index == 0) {
              this.head = this.head.next
            } else {
              var i = 0
              var current = this.head
              var pro = null
              while (i++ < index) {
                pro = current
                current = current.next
                console.log(pro)
              }
              pro.next = current.next
            }
            this.length -= 1
            return true
          }
  • 相关阅读:
    用hmac验证客户端的合法性
    初级版python登录验证,上传下载文件加MD5文件校验
    用python实现一个简单的聊天功能,tcp,udp,socketserver版本
    用struct模块解决tcp的粘包问题
    最简单的socket通信
    python中的单例模式
    python中的反射
    前端工程化思想
    h5移动端flexible源码适配终端解读以及常用sass函数
    Vue生命周期函数详解
  • 原文地址:https://www.cnblogs.com/finghi/p/15240239.html
Copyright © 2011-2022 走看看