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

1、用JQ实现堆栈。

堆栈进行的是先进后出的操作



<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<script src="../../../H5/jquery-3.1.1.min.js"></script>
	<title>Document</title>
	<script>
		$(function(){	
			function initStack(){				
				this.head = -1;//设置头部
				this.tail = -1;//设置尾部
				this.arr = new Array();	

				//压栈
				this.pushStack = function(num){	
					this.head++;
					this.arr[this.head] = num;			
				}

				//出栈
				this.popStack = function(){
					if(this.isempty()){
						console.log("堆栈为空!!")
					}
					else{
						this.arr[this.head] = null;
						this.head--;
					}
				}

				//判断栈是否为空
				this.isempty = function(){
					if(this.head == this.tail){
						return true;
					}
					else
						return false;
				}
				//删除栈
				this.del = function(){
					if(this.isempty()){
						console.log("没东西让你删了!!")
					}
					else{
						while(!this.isempty()){
							this.popStack();
						}
					}
				}
			}	
			var one = new initStack();
			$("#push").click(function(event) {
				one.pushStack($('#num').val());
				$('#display').text(one.arr);
			});
			$("#pop").click(function(event) {
				one.popStack();
				$('#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="push">
		<input type="button" value="出栈" id="pop">
		<input type="button" value="删除" id="del">
	</form>
	<div id="display">
		
	</div>
</body>
</html>

之所以使用js这种语言,纯粹是为了锻炼自己在不同语言环境下的适应能力~~



<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<script src="../../../H5/jquery-3.1.1.min.js"></script>
	<title>Document</title>
	<script>
		$(function(){	
			function initStack(){				
				this.head = -1;//设置头部
				this.tail = -1;//设置尾部
				this.arr = new Array();	

				//压栈
				this.pushStack = function(num){	
					this.head++;
					this.arr[this.head] = num;			
				}

				//出栈
				this.popStack = function(){
					if(this.isempty()){
						console.log("堆栈为空!!")
					}
					else{
						this.arr[this.head] = null;
						this.head--;
					}
				}

				//判断栈是否为空
				this.isempty = function(){
					if(this.head == this.tail){
						return true;
					}
					else
						return false;
				}
				//删除栈
				this.del = function(){
					if(this.isempty()){
						console.log("没东西让你删了!!")
					}
					else{
						while(!this.isempty()){
							this.popStack();
						}
					}
				}
			}	
			var one = new initStack();
			$("#push").click(function(event) {
				one.pushStack($('#num').val());
				$('#display').text(one.arr);
			});
			$("#pop").click(function(event) {
				one.popStack();
				$('#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="push">
		<input type="button" value="出栈" id="pop">
		<input type="button" value="删除" id="del">
	</form>
	<div id="display">
		
	</div>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值