javascript单向链表

 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);
             // 判断添加的是不是第一个节点
            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 listString = '';
             while(current) {
                listString += current.data +" ";
                current = current.next;
             }
             return listString;
         }

         LinkedList.prototype.insert = function(position,data) {
             // 越界判断
             if(position < 0 && position > this.length) {
                 return false;
             }
             var newNode = new Node(data);
             // 插入位置为0
             if(position == 0) {
                 newNode.next = this.head;
                 this.head = newNode;
             } else {
                 var index = 0;
                 var current = this.head;
                 var previous = null; 
                 while(index++ < position) {
                     previous = current;
                    current = current.next;
                 }
                 newNode.next = current;
                 previous.next = newNode;
             }
             this.length += 1;
             return true;
         }

         LinkedList.prototype.get = function(position) {
             // 越界判断
             if(position < 0 || position > this.length - 1) return null;
             var current = this.head;
             var index = 0;
             while(index++ < position) {
                current = current.next;
             }
             return current.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;
             }
             return -1;

         }

         LinkedList.prototype.update = function(position,newData) {
             // 越界判断
             if(position < 0 || position >= this.length ) return null;
             var current = this.head;
             var index = 0;
             while(index++ < position) {
                current = current.next;
             }
            current.data = newData;
            return true;

         }

         
         LinkedList.prototype.removeAt = function(position) {
             // 越界判断
             if(position < 0 && position >= this.length) {
                 return null;
             }
             // 删除位置为0
             var current = this.head;
             if(position == 0) {
                 this.head = this.head.next;
             } else {
                 var index = 0;

                 var previous = null; 
                 while(index++ < position) {
                    previous = current;
                    current = current.next  
                 }
                 // 前一个节点的next指向下一个节点的next
                 previous.next = current.next;
             }
             this.length -= 1;
             return current.data;
         }

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

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

     var linkedList = new LinkedList();
     linkedList.append('abc');
     linkedList.append('bcd');
     linkedList.append('cde');

    //  linkedList.insert(0,'aaaa');
    //  linkedList.insert(3,'bbbb');
    //  linkedList.insert(5,'ccccc');
    // linkedList.update(3,'liuning');
    //linkedList.removeAt(1);
   // linkedList.remove("abc");
   //   alert(linkedList.toString());

    // alert(linkedList.get(0));
    // alert(linkedList.get(3));
    // alert(linkedList.get(5));
   
   // alert(linkedList.indexOf("bcd"));
   alert(linkedList.isEmpty());
   alert(linkedList.size());

 

posted @ 2020-06-13 16:06  bradleydan  阅读(54)  评论(0)    收藏  举报