Skip to content

Commit

Permalink
implement stack JS
Browse files Browse the repository at this point in the history
  • Loading branch information
priyaraj7 committed Feb 8, 2022
1 parent 2fcd2b6 commit f9c3e0a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
78 changes: 75 additions & 3 deletions data-structures/stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ Explain what a stack data structure is and show how it is implemented.
[Lesson slides](https://docs.google.com/presentation/d/1lOqqqXF-NYzFw0Cu3vIa-dLZeERGhcg1SFmOgW4Y62w/edit#slide=id.p)
[Lesson video](https://drive.google.com/open?id=1ioFhuH4I0J5gAnwyw6SJxWzioAWKNrZp)

Make sure to mention these things:
### What is Stack?

- Explain what LIFO and FILO means.
- Differentiate stack and queue.
A stack is a basic linear data structure, in which the insertion and deletion of items happens at one end called top of the stack. It follows the order of LIFO (last in first out) or FILO (first in last out), the last element added to the stack will be the first element removed from the stack. The classic real-life example for stack is the stack of plates in a buffet, the plate at the top is always the first one to be removed.[^1]

![](stack.png)
_[Photo credit: programiz](https://www.programiz.com/dsa/stack)_

### Common Mistakes / Misconceptions

Expand All @@ -56,6 +58,74 @@ Explain and discuss as a class the steps involved in writing a stack structure,
- Pop/Dequeue
- Size control

```js
// program to implement stack data structure

// Class stack is declared to initialize an array that will be used to store items of the stack:
class Stack {
constructor() {
this.items = [];
}

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

// removes the last item added in the stack:
remove() {
if (this.items.length > 0) {
return this.items.pop();
}
}

// Get the topmost element of the stack
peek() {
return this.items[this.items.length - 1];
}

// checks whether or not the stack is empty
isEmpty() {
return this.items.length == 0;
}

// the size of the stack
size() {
return this.items.length;
}

// empty the stack
clear() {
this.items = [];
}
}
```

You can test the code by creating a new object of Stack class instance and call the methods from it:

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

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

console.log(stack.peek()); // // 3

console.log(stack.isEmpty()); // false

console.log(stack.size()); // 3

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).

### Independent Practice

Try to write a Stack class with the steps discussed as methods:
Expand Down Expand Up @@ -93,3 +163,5 @@ Next, ask each other the following questions:

- [GeeksforGeeks: Implementation of Stack in JavaScript](https://www.geeksforgeeks.org/implementation-stack-javascript/)
- [InitJS: Implement a Stack in JavaScript](https://initjs.org/data-structure-stack-in-javascript-714f45dbf889)

[^1]: https://betterprogramming.pub/stacks-in-javascript-d2f0e4404eac
Binary file added data-structures/stack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f9c3e0a

Please sign in to comment.