Skip to content

Commit

Permalink
change function name
Browse files Browse the repository at this point in the history
  • Loading branch information
priyaraj7 committed Feb 8, 2022
1 parent 1c9f144 commit 30908ad
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions data-structures/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ class Stack {
}

// add element to the stack
add(element) {
push(element) {
return this.items.push(element);
}

// removes the last item added in the stack:
remove() {
pop() {
if (this.items.length > 0) {
return this.items.pop();
}
Expand Down Expand Up @@ -105,13 +105,13 @@ You can test the code by creating a new object of Stack class instance and call

```js
let stack = new Stack();
stack.add(1);
stack.add(2);
stack.add(3);
stack.add(4);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
console.log(stack.items); // [ 1, 2, 3, 4 ]

console.log(stack.remove()); // 4
console.log(stack.pop()); // 4

console.log(stack.items); // [ 1, 2, 3]

Expand All @@ -125,7 +125,7 @@ stack.clear();
console.log(stack.items); // []
```

Runtime Complexity of add() and remove()is Constant, since we are using the built-in [Array.push](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) and [Array.pop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop). Both have a runtime of O(1).
Runtime Complexity of push() and pop() is Constant time, since we are using the built-in [Array.push](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) and [Array.pop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop). Both have a runtime of O(1).

### Independent Practice

Expand All @@ -136,10 +136,15 @@ function reverse(str) {
let stack = [];
// push letter into stack

// implement code here....

// pop letter from the stack
let reverseStr = '';

// implement code here....

return reverseStr;
}

console.log(reverse('I Love Stack')); // kcatS evoL I
```
Expand Down

0 comments on commit 30908ad

Please sign in to comment.