function Stack() {
this.top = null;
this.size = 0;
}
Stack.prototype = {
constructor: Stack,
push: function(data) {
if (data == null) {
return false;
} else {
var Node = {
data: data,
next: null
};
Node.next = this.top;
this.top = Node;
this.size++;
}
},
pop: function() {
if (this.size == 0) {
return null;
} else {
var data = this.top.data;
this.top = this.top.next;
this.size--;
return data;
}
}
};
var Stack = new Stack();
Stack.push(1);
Stack.push(2);
Stack.pop();javascript 链式栈
最新推荐文章于 2022-11-26 17:37:36 发布
本文介绍了一个简单的JavaScript栈数据结构实现方法,包括push和pop操作。通过构造函数创建栈实例,并演示了如何进行元素的压栈和出栈。
1027

被折叠的 条评论
为什么被折叠?



