forked from super30admin/PreCourse-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise_1.js
68 lines (57 loc) · 1.58 KB
/
Exercise_1.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
59
60
61
62
63
64
65
66
67
68
//Time Complexity to access/insert/delete data into stack is O(1)
//Space grows depending on the data hence it will be O(n)
class Stack {
constructor() {
this.items = []
this.top = null
}
isEmpty() {
return this.items.length === 0
}
size() {
return this.items.length
}
peek() {
if (this.isEmpty()) {
return "No Elements in Stack"
} else {
return "Top of the stack is - " + this.top
}
}
pop() {
if (this.isEmpty()) {
return "Insert an element before deleting it"
} else {
if (this.size === 1) {
this.top = null;
return this.items.pop();
} else {
this.top = this.items[this.items.length - 2];
return this.items.pop() && "Elements in the stack - " + this.items;
}
}
}
push(element) {
this.items.push(element)
this.top = element
return "Elements in the stack - " + this.items
}
}
const myStack = new Stack()
//Checking if the stack is empty
console.log(myStack.isEmpty())
//Checking if it throws an empty stack error
console.log(myStack.pop())
//Pushing elements into the stack
console.log(myStack.push(1))
console.log(myStack.push(2))
console.log(myStack.push(3))
console.log(myStack.push(4))
//Accessing the top element of the stack
console.log(myStack.peek())
//Deleting the stack from top
console.log(myStack.pop())
console.log(myStack.pop())
console.log(myStack.pop())
console.log(myStack.pop())
console.log(myStack.pop())