push和pop方法之外,堆栈还有其他有用的方法。让我们为我们的堆栈类添加一个peek , isEmpty和clear方法。说明编写一个push方法,将元素推送到堆栈顶部,一个pop方法删除堆栈顶部的元素,一个peek堆栈中第一个元素的peek方法,一个isEmpty方法,用于检查是否存在stack是空的,是一个clear堆栈中所有元素的方法。通常堆栈没有这个,但我们添加了一个控制台记录集合的print助手方法。 Stack类应该有一个push方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.push === "function")}()), "Your Stack class should have a push method.");'
- text: 你的Stack类应该有一个pop方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.pop === "function")}()), "Your Stack class should have a pop method.");'
- text: 你的Stack类应该有一个peek方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.peek === "function")}()), "Your Stack class should have a peek method.");'
- text: 您的Stack类应该有一个isEmpty方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.isEmpty === "function")}()), "Your Stack class should have a isEmpty method.");'
- text: 你的Stack类应该有一个clear方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.clear === "function")}()), "Your Stack class should have a clear method.");'
- text: peek方法应该返回堆栈的顶部元素
testString: 'assert((function(){var test = new Stack(); test.push("CS50"); return (test.peek() === "CS50")}()), "The peek method should return the top element of the stack");'
- text: pop方法应该删除并返回堆栈的顶部元素
testString: 'assert((function(){var test = new Stack(); test.push("CS50"); return (test.pop() === "CS50");}()), "The pop method should remove and return the top element of the stack");'
- text: 如果堆栈不包含任何元素,则isEmpty方法应返回true
testString: 'assert((function(){var test = new Stack(); return test.isEmpty()}()), "The isEmpty method should return true if a stack does not contain any elements");'
- text: clear方法应该从堆栈中删除所有元素
testString: 'assert((function(){var test = new Stack(); test.push("CS50"); test.clear(); return (test.isEmpty())}()), "The clear method should remove all element from the stack");'
```