js优先队列和链表

 //封装优先级队列
    function PriorityQueue(){
        function QueueElement(element,priority){
           this.element=element
           this.priority=priority
        }

       //封装属性
       this.items=[]

       //实现插入方法
        PriorityQueue.prototype.enqueue=function(element,priority){
            // 1.创建PriorityQueue对象
            var queueElement=new QueueElement(element,priority)

            //2.判断队列是否为空
            if(this.items.length===0){
                this.items.push(queueElement)
            }else{
                var added=false
                for(var i=0;i<this.items.length;i++){
                    if(queueElement.priority< this.items[i].priority){
                        this.items.splice(i,0,queueElement)
                        added=true
                        break
                    }
                }

                if(!added){
                    this.items.push(queueElement)
                }
            }
        }

        PriorityQueue.prototype.dequeue=function(){
            return this.items.shift()
        }

        PriorityQueue.prototype.front=function(){
            return this.items[0]
        }

        PriorityQueue.prototype.isEmpty=function(){
            return this.items.length===0
        }

        PriorityQueue.prototype.size=function(){
            return this.items.length
        }

        PriorityQueue.prototype.toString=function(){
            var resultString=''
            for(var i=0;i<this.items.length;i++){
                resultString+=this.items[i].element+'-'+this.items[i].priority+' '
            }
            return resultString
        } 
    }


    //测试代码
    var pq=new PriorityQueue
    pq.enqueue('abc',111)
    pq.enqueue('cab',200)
    pq.enqueue('nba',50)
    pq.enqueue('nba',66)

    alert(pq)

  链表的相关方法:

 单链表 //分装链表类    function LinkedList(){

        //内部的类:节点类
        function Node(data){
            this.data=data
            this.next=null
        }
        //属性
        this.head=null
        this.length=0

        //1.追加方法
        LinkedList.prototype.append=function(data){
            //1.创建新的节点
            var newNode=new Node(data)
            //判断是否添加的是第一个节点
            if(this.length===0){ //是第一个节点
                this.head=newNode
            }else{
                //找到最后一个节点
                var current=this.head
                while(current.next){
                    current=current.next
                }
                //最后节点的 next指向新的节点
                current.next=newNode
            }
            //3.lenght +1
            this.length+=1
        }
    
       //2.toString方法
       LinkedList.prototype.toString=function(){
           //1.定义变量
           var current=this.head
           var listString=""

           //2.循环获取一个个节点
           while(current){
               listString+=current.data+  "  "
               current=current.next
           }

           return listString
       }

       //3.insert方法
       LinkedList.prototype.insert=function(position,data){
           //1.对 position进行越界判读
           if(position<0||position>this.length) return false

           //2.根据 data 创建newNode
           var newNode=new Node(data)

           //3.判断插入的位置是否是第一个
           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
           }

           //4.length+1
           this.length+=1
           return true
       }

       //4.get 方法
       LinkedList.prototype.get=function(position){
           //1.越界判断
           if(position<0||position>=this.length) return null

           //2.获取对应的数据
           var current=this.head
           var index=0
           while(index++<position){
              current=current.next
           }
           return current.data

       }

  

//5.indexOf
       LinkedList.prototype.indexOf=function(data){
          //1.定义变量
           var current=this.head
           var index=0

           //2.开始查找
           while(current){
               if(current.data===data){
                   return index
               }
               current=current.next
               index+=1
           }

           //3.找到最后没有找到,返回-1
           return -1
       }

       //6.update方法
       LinkedList.prototype.updated=function(position,newData){
           //1.越界判断
           if(position<0||position>=this.length) return false

           //2.查找正确的节点
           var current=this.head
           var index=0
           while(index++<position){
               current=current.next
           }

           //3.position位置的 node 的 data修改为 newData
           current.data=newData
           return true
       }

       //7.removeAt方法
       LinkedList.prototype.removeAt=function(position){
           //1.越界判断
           if(position<0||position>=this.length) return null

           //2.判断是否删除的是第一个节点
           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指向,current的 next 即可
               previous.next=current.next 
           }
           //3.length-1
           this.length-=1
           return current.data
       }

       //8.remove方法
       LinkedList.prototype.remove=function(data){
           //1.获取data在列表中的位置
           var position=this.indexOf(data)

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

       //9.isEmpty方法
       LinkedList.prototype.isEmpty=function(){
           return this.length===0
       }

       //10.size方法
       LinkedList.prototype.size=function(){
           return this.length
       }
}

  

 

 双线链表封装:

 function DoublyLinkedList(){
        //内部类
        function Node(data){
            this.data=data
            this.prev=null
            this.next=null
        }
        //属性
        this.head=null
        this.tail=null
        this.length=0

        //常见的操作
        //1.append方法
        DoublyLinkedList.prototype.append=function(data){
            //1.根据 data 创建节点
            var newNode=new Node(data)

            //2.判断添加的是否是第一个节点
            if(this.length===0){
                this.head=newNode
                this.tail=newNode
            }else{
                newNode.prev=this.tail
                this.tail.next=newNode
                this.tail=newNode
            }

            //3.length+1
            this.length+=1
        }

        //2.将链表转成字符串形式

        //2.1 toString方法
        DoublyLinkedList.prototype.toString=function(){
            return this.backwardString()
        }

        //2.2forwardString 方法
        DoublyLinkedList.prototype.forwardString=function(){
            //1.定义变量
            var current=this.tail
            var resultString=""

            //2.依次向前遍历,获取每一个节点
            while(current){
                resultString+=current.data+" "
                current=current.prev
            }

            return resultString
        }

        //2.3backwardString 方法
        DoublyLinkedList.prototype.backwardString=function(){
            //1.定义变量
            var current=this.head
            var resultString=""

            //2.依次向后遍历,获取每一个节点
            while(current){
                resultString+=current.data+" "
                current=current.next
            }
            return resultString
        }

        //3.insert 方法
        DoublyLinkedList.prototype.insert=function(position,data){
            //1.越界判断
            if(position<0||position>this.length) return false

            //2.根据 data 创建新的方法
            var newNode=new Node(data)

            //3.判断原来的列表是否为空
            if(this.length===0){
                this.head=newNode
                this.tail=newNode
            }else{
                if(position===0){
                   //3.1判断 position是否为空
                   this.head.prev=newNode
                   newNode.next=this.head
                   this.head=newNode
                }else if(position===this.length){
                   //3.2position===this.length
                   newNode.prev=this.tail
                   this.tail.next=newNode
                   this.tail=newNode
                }else{
                    //3.3其他情况
                    var current=this.head
                    var index=0

                    while(index++ < position){
                        current=current.next
                    }

                   //4.修改指针
                   newNode.next=current
                   newNode.prev=current.prev
                   current.prev.next=newNode
                   current.prev=newNode
                }
            }
            //4.length+1
            this.length+=1
            return true       
        }

        //4.get 方法
        DoublyLinkedList.prototype.get=function(position){
            //1.越界判断
            if(position<0||position>this.length) return null

            //2.获取元素
            var current=this.head
            var index=0
            while(index++<position){
                current=current.next
            }
            return current.data
        }
    
        //5.indexOf 方法
        DoublyLinkedList.prototype.indexOf=function(data){
            //1.定义变量
             var current=this.head
             var index=0

             //2.查找和 data相同的节点
             while(current){
                 if(current.data===data){
                     return index
                 }
                 current=current.next
                 index+=1
             }
             return -1
        }

        //6.update方法
        DoublyLinkedList.prototype.update=function(position,newData){
            //1.越界判断
            if(position<0||position>=this.length) return false

            //2.寻找正确的节点
            var current=this.head
            var index=0
            while(index++<position){
                current=current.next
            }

            //3.修改找到节点的 data 信息
            current.data=newData
            return true
        }

        //7.removeAt 方法
        DoublyLinkedList.prototype.removeAt=function(position){
            //1.越界判断
            if(position<0||position>=this.length) return null

            //2.判断列表中是否只有一个节点
            var current=this.head
            if(this.length===1){
                this.head=null
                this.tail=null
            }else{
                //判断是否删除的是第一个节点
                if(position===0){
                    this.head.next.prev=null
                    this.head=this.head.next
                }else if(position===this.length-1){//最后节点
                    current=this.tail
                    this.tail.prev.next=null
                    this.tail=this.tail.prev
                }else{
                    var index=0
                
                    while(index++<position){
                       current=current.next
                    }

                    current.prev.next=current.next
                    current.next.prev=current.prev
                }
            }

            //3.length-1
            this.length-=1
            return current.data
        }

        //8.remove 方法
        DoublyLinkedList.prototype.remove=function(data){
            //1.根据 data 获取下标值
            var index=this.indexOf(data)

            //2.根据index 删除对应位置的节点
            return this.removeAt(index)
        }

        //9.isEmpty方法
        DoublyLinkedList.prototype.isEmpty=function(){
            return this.length===0
        }

        //10.size方法
        DoublyLinkedList.prototype.size=function(){
            return this.length
        }

        //11.获取链表的第一个元素
        DoublyLinkedList.prototype.getHead=function(){
            return this.head.data
        }

        //12.获取链表的最后一个元素
        DoublyLinkedList.prototype.getTail=function(){
            return this.tail.data
        }
    }
  
    //测试代码
    var list=new DoublyLinkedList()

    //1.测试 append方法
    list.append('abc')
    list.append('cba')
    list.append('nba')

    //2.测试转成字符串的方法
    // alert(list)
    // alert(list.backwardString())
    // alert(list.forwardString())

    //3.测试 insert方法
    list.insert(0,'aaa')
    list.insert(4,'bbb')
    list.insert(2,'ccc')
    //alert(list)

    //4.测试 get 方法
    // alert(list.get(0))
    // alert(list.get(2))
    // alert(list.get(5))

    //5.测试 indexOf
    // alert(list.indexOf('aaa'))
    // alert(list.indexOf('abc'))
    // alert(list.indexOf('nba'))

    //6.测试 update方法
    list.update(0,'mmm')
    list.update(1,'nnn')
    alert(list)

    //7.测试 removeAt 方法
    alert(list.removeAt(1))
    alert(list)

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

    //8.测试 remove 方法
    alert(list.remove('cba'))
    alert(list)
    alert(list.remove('nba'))
    alert(list)

    //9.测试其他方法
    alert(list.isEmpty())
    alert(list.size())
    alert(list.getHead())
    alert(list.getTail() )

  

原文地址:https://www.cnblogs.com/sunliyuan/p/13737052.html