1.栈
-
后进先出
-
函数是进行栈中
1.1栈的实现方式
-
数组
-
链表
1.2栈的常见操作
-
push(element):添加一个新元素到栈顶位置
-
pop():移除栈顶的元素,同时返回被移除的元素
-
peek():返回栈顶的元素,不对栈做任何修改
-
isEmpty():如果栈里没有任何元素就返回true,否则返回false
-
size()返回栈里的元素个数,这个方法和数组的length属性类似
-
toString():将栈结构的内容以字符形式返回
1.3栈操作的实践
//封装栈类
function Stack () {
//栈中的属性
this.items = []
//栈的相关操作
//1.将元素压入栈
Stack.prototype.push = function (e) {
this.items.push(e)
}
//2.将元素出栈
Stack.prototype.pop = function () {
return this.items.pop()
}
Stack.prototype.peek = function () {
return this.items[this.items.length - 1]
}
Stack.prototype.isEmpty = function () {
if (this.items.length == 0)
return false
else return true
}
Stack.prototype.size = function () {
return this.items.length
}
Stack.prototype.toString = function () {
return this.items.toString()
}
}
stack = new Stack()
[类] 在js中类的方法最好是写在原型链中如:Stack.prototype.方法=function(){}