[js]①栈和队列~2、简单队列--数据结构回忆小笔记

队列遵从先进先出原则~没啥好说,直接附上代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<script src="../../../H5/jquery-3.1.1.min.js"></script>
	<script>
		$(function(){

			function initQueue(){
				this.front = 0;
				this.rear = 0;
				this.arr = new Array();
				//入队列
				this.inQueue = function(num){
					this.arr[this.rear++] = num;
				}	

				//出队列
				this.outQueue = function(){
					if(this.isempty()){
						alert("队列为空!!")
					}
					else{
						this.arr.splice(this.front,1);
						this.rear--;
					}
				}	

				//判断是否为空
				this.isempty = function(){
					if(this.front == this.rear){
						return true;
					}
					else{
						return false;
					}

				}
				this.del = function(){
					if(this.isempty()){
						alert("队列已为空!!")
					}
					else{
						while(!this.isempty()){
							this.outQueue();
						}
					}
				}
			}

			var one = new initQueue();
			$("#in").click(function(event) {
				one.inQueue($('#num').val());
				console.log(one.arr);
				$('#display').text(one.arr);
			});
			$("#out").click(function(event) {
				one.outQueue();
				console.log(one.arr);
				$('#display').text(one.arr);			
			});
			$("#del").click(function(event) {
				one.del();
				$('#display').text(one.arr);			
			});

		})
	</script>
	<style>
		*{
			margin: 0;
			padding: 0;	
		}
	</style>
</head>
<body>
	<form action="">
		<input type="text" id="num">
		<input type="button" value="进队列" id="in">
		<input type="button" value="出队列" id="out">
		<input type="button" value="删除" id="del">
	</form>
	<div id="display">
		
	</div>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值