aardio数据结构之栈结构

axuanup 4月前 396

Code here

//stack栈结构
//栈的特点:先进后出
import console;
class stack{
	ctor(){
		this.items = {};
	};
	//入栈
	push = function(element){
		..table.push(this.items,element);
	}
	//出栈
	pop = function(){
		return ..table.pop(this.items);
	}
	//末位
	peek = function(){
		return this.items[#this.items]; 
	}
	//是否为空栈
	isEmpty = function(){
		return !#this.items; 
	}
	
	//大小
	size = function(){
		return #this.items; 
	}
	
	//清空栈
	clear = function(){
		this.items = {}; 
	}	
}

//实例化栈
var Stack = stack();

//入栈
Stack.push("A");
Stack.push("B");
Stack.push("C");
Stack.push("D");

//出栈
Stack.pop()	

//末位
console.log(Stack.peek())

//大小
console.log(Stack.size())

//是否为空栈
console.log(Stack.isEmpty())

console.log(Stack.clear())

//大小
console.log(Stack.size())

console.pause(true);



最新回复 (1)
  • 光庆 4月前
    0 2

返回