数据结构14-栈常见操作3
2025-04-01 09:21:49 阅读次数:3
html,javascript,Stack,前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>栈的封装</title>
</head>
<body>
<script>
function Stack(){
this.items=[]
Stack.prototype.push=function(element){
this.items.push(element)
}
Stack.prototype.pop=function(element){
return this.items.pop(element)
}
Stack.prototype.peek=function(element){
return this.items[this.items.length-1]
}
Stack.prototype.isEmpty=function(element){
return this.items.length==0
}
Stack.prototype.size=function(element){
return this.items.length
}
Stack.prototype.toString=function(element){
var resultString=""
for(var i=0;i<this.items.length;i++){
resultString+=this.items[i]+""
}
return resultString
}
}
</script>
</body>
</html>
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/u_15460007/6050465,作者:前端导师歌谣,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。
上一篇:数据字典的实现与应用 —— 提高系统灵活性与维护效率的关键
下一篇:TypeScript常用知识点整理