2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Learn how a Stack Works
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Learn how a Stack Works
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Problem Explanation
|
2018-10-12 15:37:13 -04:00
|
|
|
- Stacks are an abstract data structures.
|
|
|
|
- They follow LIFO (Last In First Out) or FILO (First In Last Out) principle.
|
|
|
|
- Stack's insertion and deletion operations are of **O(1)** time complexity.
|
2019-03-28 09:35:41 +01:00
|
|
|
- In JavaScript, arrays can be treated as a Stack since `.push()` and `.pop()` methods have time complexity of **O(1)**.
|
2018-10-12 15:37:13 -04:00
|
|
|
- In this challenge we need to `.pop()` and then `.push()` into the stack.
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## Solutions
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
```js
|
2019-07-24 00:59:27 -07:00
|
|
|
var homeworkStack = ["BIO12", "HIS80", "MAT122", "PSY44"];
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
homeworkStack.pop();
|
|
|
|
homeworkStack.push("CS50");
|
|
|
|
```
|
2019-07-24 00:59:27 -07:00
|
|
|
#### Relevant Links
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
- [Wikipedia](https://en.wikipedia.org/wiki/Stack_(abstract_data_type))
|
|
|
|
- Video by [Hackerrank](https://www.youtube.com/watch?v=wjI1WNcIntg)
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
</details>
|