push
e pop
, as pilhas possuem outros métodos úteis. Vamos adicionar um método peek
, isEmpty
e clear
a nossa classe stack. Instruções Escreva um método push
que empurre um elemento para o topo da pilha, um método pop
que remova o elemento na parte superior da pilha, um método de peek
que examine o primeiro elemento na pilha, um método isEmpty
que verifique se o elemento pilha está vazia e um método clear
que remove todos os elementos da pilha. Normalmente as pilhas não têm isso, mas adicionamos um método auxiliar de print
que o console registra na coleção. Stack
deve ter um método push
.
testString: 'assert((function(){var test = new Stack(); return (typeof test.push === "function")}()), "Your Stack
class should have a push
method.");'
- text: Sua classe Stack
deve ter um método pop
.
testString: 'assert((function(){var test = new Stack(); return (typeof test.pop === "function")}()), "Your Stack
class should have a pop
method.");'
- text: Sua classe Stack
deve ter um método de peek
.
testString: 'assert((function(){var test = new Stack(); return (typeof test.peek === "function")}()), "Your Stack
class should have a peek
method.");'
- text: Sua classe Stack
deve ter um método isEmpty
.
testString: 'assert((function(){var test = new Stack(); return (typeof test.isEmpty === "function")}()), "Your Stack
class should have a isEmpty
method.");'
- text: Sua classe Stack
deve ter um método clear
.
testString: 'assert((function(){var test = new Stack(); return (typeof test.clear === "function")}()), "Your Stack
class should have a clear
method.");'
- text: O método peek
deve retornar o elemento principal da pilha
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: O método pop
deve remover e retornar o elemento principal da pilha
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: O método isEmpty
deve retornar true se uma pilha não contiver nenhum elemento
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: O método clear
deve remover todos os elementos da pilha
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");'
```