push
и pop
, у стеков есть и другие полезные методы. Давайте добавим peek
, isEmpty
и clear
метод в наш класс стека. Инструкции Напишите метод push
который подталкивает элемент к вершине стека, метод pop
который удаляет элемент в верхней части стека, метод peek
который смотрит на первый элемент в стеке, метод isEmpty
который проверяет, стек пуст и 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");'
```