js数据结构--队列

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>


<script type="text/javascript">
    1.队列(FIFO):先进先出
    2.创建一个类表示队列
        function Queue(){
            //这里是属性和方法
        }
    3.使用数组来存储队列中的元素
        var items=[];
    4.声明队列的方法
        enqueue(elements):向队列尾部添加一个或多个新元素
        dequeue():移除队列第一个元素,并返回移除的元素
        front():返回添加到队列中的第一个元素
        isEmpty():如果队列中元素为空,返回true,否则返回flase
        size():返回队列中元素的个数
    5.实现enqueue(elements)方法
        this.enqueue=function(elements){
            items.push(elements);
        }
    6.实现dequeue()方法
        this.dequeue=function(){
            return items.shift();
        }
    7.实现front()方法
        this.front=function(){
            return items[0];
        }
    8.实现isEmpty()方法
        this.isEmpty=function(){
            return items.length==0;
        }
    9.实现size()方法
        this.size=function(){
            return items.length;
        }
    10.实现print()方法
        this.print=function(){
            console.log(items.toString());
        }
    11.实现cleat()方法
        this.clear=function(){
            items=[];
        }
    12.队列的全部代码
        function Queue(){
            var items=[];

            function enqueue(elements){
                items.push(elements);
            };

            function dequeue(){
                return items.shift();
            };

            function front(){
                return items[0];
            };

            function isEmpty(){
                return items.length==0;
            };

            function size(){
                return items.length;
            };

            function print(){
                console.log(items.toString());
            };

            function clear(){
                items=[];
            };
        }
    -------------------------------------------
    使用Queue类

    1.实例化Queue类,并判断是否为空
        var queue=new Queue();
        console.log(queue.isEmpty());    //true
    2.添加一些元素
        queue.enqueue('join');
        queue.enqueue('jack');
        queue.enqueue('caml');
        queue.print();
        console.log(queue.size());    //3
        console.log(queue.isEmpty());    //false
        queue.dequeue();
        queue.dequeue();
        queue.print();
</script>
</body>
</html>

 

posted @ 2017-11-07 18:40  逗比煎饼侠  阅读(161)  评论(0编辑  收藏  举报