-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathstack1.js
58 lines (51 loc) · 1.46 KB
/
stack1.js
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
/**
* Implemntation of Stack Data Structure in JavaScript
* @author MadhavBahl
* @date 11/02/2019
* Method 1 - Using JavaScript's push and pop methods
*/
class Stack {
// constructor to initialize the stack and it's capacity
constructor (limit) {
this.myStack = [];
this.top = limit;
}
// isEmpty method to check whether the stack is empty
isEmpty () {
if (this.myStack.length === 0) return true;
else return false;
}
// isFull method to check whether the stack is full
isFull () {
if (this.myStack.length === this.top) return true;
else return false;
}
// push method to add a record to the stack
push (record) {
if (!this.isFull()) {
console.log (`Pushing ${record} to the stack!`);
this.myStack.push (record);
} else {
console.log ('Sorry! The Stack is full!');
}
}
// pop method to remove an element from the stack
pop () {
if (!this.isEmpty()) {
console.log (`Popped element is: ${this.myStack[this.myStack.length-1]}`);
return this.myStack.pop ();
} else {
console.log ('Sorry! The Stack is empty');
}
}
// peek method to view the top element
peek () {
console.log (`Current element is: ${this.myStack[this.myStack.length - 1]}`);
}
}
const stk = new Stack (10);
stk.pop ();
stk.push (1);
stk.push (2);
stk.pop ();
stk.peek ();