zoukankan      html  css  js  c++  java
  • 【javascript】数据结构-队列

    <!DOCTYPE html>
    <html>
    <head>
    	<title>queue</title>
    	<meta charset="utf-8">
    	<script type="text/javascript">
    		function Queue(){
    			// 用于存储队列的数据结构
    			var items = [];
    
    			// enqueue:向队列末尾添加一个或多个新的项
    			this.enqueue = function(element){
    				items.push(element);
    			};
    
    			// dequeue:移除队列的第一项,并返回被移除的元素
    			this.dequeue = function(){
    				items.shift();
    			};
    
    			// front:返回队列中的第一个元素
    			this.front = function(){
    				return items[0];
    			};
    
    			// isEmpty:如果队列中不包含任何元素,返回true,否则返回false
    			this.isEmpty = function(){
    				return items.length == 0;
    			};
    
    			// clear:清空队列
    			this.clear = function(){
    				items = [];
    			};
    
    			// size:返回队列中包含的元素个数,与数组的length方法类似
    			this.size = function(){
    				return items.length;
    			};
    
    			// print:打印队列元素
    			this.print = function(){
    				console.log(items.toString());
    			};
    		}
    
    		// 队列的使用
    		// 创建队列
    		var queue = new Queue();
    		console.log(queue.isEmpty());		//输出true
    
    		// 给队列添加元素
    		queue.enqueue("John");
    		queue.enqueue("Jack");
    		queue.enqueue("Alice");
    
    		// 打印队列
    		queue.print();						//输出 John,Jack,Alice
    
    		// 输出队列大小
    		console.log(queue.size());			//输出 3
    
    		// 删除队列元素
    		queue.dequeue();
    		// 打印队列
    		queue.print();						//输出 Jack,Alice
    	</script>
    </head>
    <body>
    
    </body>
    </html>
    

      

  • 相关阅读:
    HDU 2853 (KM最大匹配)
    HDU 2852 (树状数组+无序第K小)
    HDU 2851 (最短路)
    HDU 2846 (AC自动机+多文本匹配)
    MyBatis使用示例
    Hessian示例:Java和C#通信
    SQL Server2005配置同步复制
    【问】如何应对关系型数据库中列的不断增加
    Prolog学习:数独和八皇后问题
    Prolog学习:基本概念
  • 原文地址:https://www.cnblogs.com/dragonir/p/7701460.html
Copyright © 2011-2022 走看看