3.9 KiB
3.9 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d8250367417b2b2512c5f | Create a Stack Class | 1 | 创建一个堆栈类 |
Description
push
和pop
方法之外,堆栈还有其他有用的方法。让我们为我们的堆栈类添加一个peek
, isEmpty
和clear
方法。说明编写一个push
方法,将元素推送到堆栈顶部,一个pop
方法删除堆栈顶部的元素,一个peek
堆栈中第一个元素的peek
方法,一个isEmpty
方法,用于检查是否存在stack是空的,是一个clear
堆栈中所有元素的方法。通常堆栈没有这个,但我们添加了一个控制台记录集合的print
助手方法。 Instructions
Tests
tests:
- text: 你的<code>Stack</code>类应该有一个<code>push</code>方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.push === "function")}()), "Your <code>Stack</code> class should have a <code>push</code> method.");'
- text: 你的<code>Stack</code>类应该有一个<code>pop</code>方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.pop === "function")}()), "Your <code>Stack</code> class should have a <code>pop</code> method.");'
- text: 你的<code>Stack</code>类应该有一个<code>peek</code>方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.peek === "function")}()), "Your <code>Stack</code> class should have a <code>peek</code> method.");'
- text: 您的<code>Stack</code>类应该有一个<code>isEmpty</code>方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.isEmpty === "function")}()), "Your <code>Stack</code> class should have a <code>isEmpty</code> method.");'
- text: 你的<code>Stack</code>类应该有一个<code>clear</code>方法。
testString: 'assert((function(){var test = new Stack(); return (typeof test.clear === "function")}()), "Your <code>Stack</code> class should have a <code>clear</code> method.");'
- text: <code>peek</code>方法应该返回堆栈的顶部元素
testString: 'assert((function(){var test = new Stack(); test.push("CS50"); return (test.peek() === "CS50")}()), "The <code>peek</code> method should return the top element of the stack");'
- text: <code>pop</code>方法应该删除并返回堆栈的顶部元素
testString: 'assert((function(){var test = new Stack(); test.push("CS50"); return (test.pop() === "CS50");}()), "The <code>pop</code> method should remove and return the top element of the stack");'
- text: 如果堆栈不包含任何元素,则<code>isEmpty</code>方法应返回true
testString: 'assert((function(){var test = new Stack(); return test.isEmpty()}()), "The <code>isEmpty</code> method should return true if a stack does not contain any elements");'
- text: <code>clear</code>方法应该从堆栈中删除所有元素
testString: 'assert((function(){var test = new Stack(); test.push("CS50"); test.clear(); return (test.isEmpty())}()), "The <code>clear</code> method should remove all element from the stack");'
Challenge Seed
function Stack() {
var collection = [];
this.print = function() {
console.log(collection);
};
// Only change code below this line
// Only change code above this line
}
Solution
// solution required