js algorithm Practice - Team

 // general queue 
        class {Queue 
            constructor () { 
                the this .list = []; 
            } 

            // enqueue 
            ENQUEUE (Item) {
                 the this .list.push (Item); 
            } 
            // dequeue 
            the Dequeue () {
                 return  the this .list .shift (); 
            } 
            // first element value 
            getFront () {
                 return  the this .list [0 ]; 
            } 
            // length 
            GetSize () {
                 return  the this .list.length; 
            }
            // is empty 
            GetIsEmpty () {
                 return  the this .list.length == 0 ; 
            } 
            // clear 
            the Clear () {
                 the this .list = []; 
            } 
            // read element 
            the Read () { 
                the console.log ( the this . list.toString ()); 
            } 
        } 

        the let QueueTest = new new Queue (); 
        queueTest.GetIsEmpty (); // to true 
        queueTest.EnQueue (. 1 ); 
        queueTest.EnQueue ( 2 );
        queueTest.EnQueue ( . 3 ); 
        queueTest.Read (); // l, 2,3 
        queueTest.Dequeue (); // 1 
        queueTest.Read (); // 2,3 

        // priority queue: 1: the elderly and pregnant women (or women with children) when boarding also enjoy priority over other passengers; 
        // doctor will give priority to more serious illness in patients with 
        class PriorityQueue { 
            constructor () { 
                the this .list = []; 
            } 

            // into team 
            ENQUEUE (Item, priority) {
                 var F = {Item: Item, index: priority};
                 IF ( the this .list.length == 0)
                    this.list.push(f);
                else {
                    var position = this.list.findIndex(x => x.index > f.index);
                    if (position == -1)
                        this.list.push(f);
                    else
                        this.list.splice(position, 0, f);
                }
            }
            //出队
            Dequeue() {
                return this.list.shift();
            }
            //第一个元素值
            GetFront() {
                return  the this .list [0 ]; 
            } 
            // length 
            GetSize () {
                 return  the this .list.length; 
            } 
            // is empty 
            GetIsEmpty () {
                 return  the this .list.length == 0 ; 
            } 
            // Clear 
            Clear () {
                 the this .list = []; 
            } 
            // read element 
            the read () { 
                the console.log ( the this .list); 
            } 
        } 
        var priorityQueue = new PriorityQueue();
        priorityQueue.GetIsEmpty();//true;
        priorityQueue.EnQueue(1, 1);
        priorityQueue.EnQueue(2, 2);
        priorityQueue.EnQueue(22, 3);
        priorityQueue.EnQueue(22, 5);
        priorityQueue.Read();//[{item:1,index:1},{item:2,index:2},{item:22,index:3},{item:22,index:5}]
        priorityQueue.EnQueue(33, 4);
        priorityQueue.Read();//[{item:1,index:1},{item:2,index:2},{item:22,index:3},{item:22,index:4},{item:22,index:5}]
        priorityQueue.Dequeue (); // {Item:. 1, index:. 1} 
        // cycle queue 
        // to make full use of vector space, to overcome the "false" Overflowing method: the vector space imagined as a butt circle rings, and said cyclic vector such as a vector. 
        the extends LoopQueue {Queue class 

            constructor (items) { 
                Super (items) 
            } 

            getIndex (index) { 
                const length = the this .list.length
                 return index> length (% index? length): index 
            } 

            Find (index) { 
                return ! the this . ? isEmpty the this .list [ the this .getIndex (index)]:null
            }
        }
        var loopQueue=new LoopQueue();
        loopQueue.EnQueue(1);
        loopQueue.EnQueue(2);
        loopQueue.EnQueue(3);
        loopQueue.Read();//1,2,3
        loopQueue.find(7);//2

 

Guess you like

Origin www.cnblogs.com/shuajing/p/11319041.html
Recommended