Skip to content

Latest commit

 

History

History
80 lines (75 loc) · 1.77 KB

javascript数据结构3-栈.md

File metadata and controls

80 lines (75 loc) · 1.77 KB
title tags categories
javascript数据结构3-栈
javascript
数据结构
数据结构

后进先出(LIFO,last-in-first-out)的数据结构 类比:堆叠盘子,只能从上面拿走盘子

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<title>栈</title>
</head>
<body>
   <script type="text/javascript">
		function Stack() {
		   this.dataStore = [];
		   this.pos = 0;
		   this.push=push;
		   this.pop=pop;
		   this.peek=peek;
		   this.clear = clear;
		   this.length=length;
		}
        
        function push(element){
        	this.dataStore[this.pos++]=element;
        }
        function peek(){
        	return this.dataStore[this.top-1];
        }
        function pop(){
        	return this.dataStore[--this.top];
        }
        function clear(){
        	this.top=0;
        }
        function length(){
        	return this.top;
        }
       /************************************************************************/
       	var s=new Stack();
       	s.push("Tom");
       	s.push("Som");
       	s.push("Dom");
       	s.push("Fom");
       	// document.write(s.dataStore);
       	console.log(s);
   </script>
</body>
</html>

例子: 十进制转化为二进制,使用栈实现:

/*数制间的相互转换*/
        function mulBase(num,base){
        	var s=new Stack();
        	do{
        		s.push(num% base);
        		num=Math.floor(num /=base);
        	}while(num > 0);
        	var cov="";
          console.log(s.length());
        	while(s.length() >0){
        		cov += s.pop();
        		
        	}
        	return cov;
        }
       var num=32;
       var newNum=mulBase(32,2);  //十进制转换为二进制
       console.log(newNum);
       document.write(newNum);