-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstack.js
More file actions
64 lines (52 loc) · 913 Bytes
/
stack.js
File metadata and controls
64 lines (52 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function Stack() {
let items = [];
/**
*
*/
this.push = (element)=>items.push(element);
/**
* Removes the last item of the stack
*/
this.pop = ()=> items.pop();
/**
* the last item added
*/
this.peek = ()=> items[items.length-1];
// console.table(items[items.length-1]);
// this.peek = ()=> items[items.length-1];
/**
*
*/
this.isEmpty = ()=> items.length == 0;
/**
*
*/
this.size = ()=> items.length;
/**
*
*/
this.clear = ()=> items = [];
/**
*
*/
this.print = ()=> console.table(items);
/**
*
*/
this.toString = ()=> items.toString();
}
/**
* STEPS
*/
// Using the stack
let stack = new Stack();
stack.push(5);
stack.push(8);
stack.print()
stack.push(11);
stack.print()
// stack.peek()
console.log(` Last item added: ${stack.peek()}` );
stack.pop();
stack.print()
console.log(` Last item added: ${stack.peek()}` );