GeezerVison is showing github.com — Fixed stack demo · prafuldaga/JavaScript-Lessons@c226b75
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +4-4Lines changed: 4 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | # Stacks |
2 | 2 |
|
3 | | -A stack is a basic data structure that can be logically thought as linear structure represented by a real physical |
4 | | -stack or pile, a structure where insertion and deletion of items takes place at one end called top of the stack. The |
5 | | -basic concept can be illustrated by thinking of your data set as a stack of plates or books where you can only take the |
6 | | -top item off the stack in order to remove things from it. |
| 3 | +A stack is a data structure that can be logically thought of as a real physical stack or pile, a structure where |
| 4 | +insertion and deletion of items takes place at one end only called the "top" of the stack. The basic concept can be |
| 5 | +illustrated by thinking of your data set as a stack of plates or books where you can only take the top item off the |
| 6 | +stack in order to remove things from it. |
7 | 7 |
|
8 | 8 |  |
Collapse file
Copy file name to clipboardExpand all lines: Data_Structures/Stacks/main.js +16-7Lines changed: 16 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
3 | 3 | function Stack() { |
4 | 4 | let items = []; |
5 | 5 |
|
6 | | - // remove all items in the stack |
| 6 | + // remove all items |
7 | 7 | this.clear = function () { |
8 | 8 | items = []; |
9 | 9 | }; |
@@ -13,25 +13,30 @@ function Stack() { |
13 | 13 | return items.length == 0; |
14 | 14 | }; |
15 | 15 |
|
16 | | - // return last item added to stack |
| 16 | + // return last item added |
17 | 17 | this.peek = function () { |
18 | 18 | return items[items.length - 1]; |
19 | 19 | }; |
20 | 20 |
|
| 21 | + // remove last item from end |
| 22 | + this.pop = function () { |
| 23 | + return items.pop(); |
| 24 | + }; |
| 25 | + |
21 | 26 | // display all items |
22 | 27 | this.print = function () { |
23 | 28 | console.log(items.toString()); |
24 | 29 | }; |
25 | 30 |
|
26 | | - // add item to end of stack |
| 31 | + // add item to end |
27 | 32 | this.push = function (element) { |
28 | 33 | items.push(element); |
29 | 34 | }; |
30 | 35 |
|
31 | | - // remove last item from stack |
32 | | - this.pop = function () { |
33 | | - return items.pop(); |
34 | | - } |
| 36 | + // get size of stack |
| 37 | + this.size = function () { |
| 38 | + return items.length; |
| 39 | + }; |
35 | 40 |
|
36 | 41 | } |
37 | 42 |
|
@@ -42,7 +47,11 @@ buckysStack.push(87); |
42 | 47 | buckysStack.push(29); |
43 | 48 | buckysStack.push(71); |
44 | 49 | buckysStack.print(); |
| 50 | + |
| 51 | +console.log('--------------------'); |
| 52 | + |
45 | 53 | console.log(buckysStack.peek()); |
| 54 | +console.log(buckysStack.size()); |
46 | 55 |
|
47 | 56 | console.log('--------------------'); |
48 | 57 |
|
|