Skip to content

Commit eafc58e

Browse files
committed
Add sort stack implementation
1 parent 1f5a376 commit eafc58e

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
class MyStack {
2+
3+
constructor() {
4+
this.array = []; // Array is used to implement stack
5+
}
6+
7+
// List of main functions of stack data structure
8+
9+
push(value) {
10+
// push an element into the array
11+
this.array.push(value);
12+
}
13+
14+
pop() {
15+
// Underflow if stack is empty
16+
if (this.isEmpty()) {
17+
return "Underflow";
18+
}
19+
20+
return this.array.pop(); // return top most element from the stack and removes the same element
21+
}
22+
23+
peek() {
24+
return this.array[this.array.length - 1]; // return top most element from the stack without removing the element
25+
}
26+
27+
// List of helper functions
28+
29+
isEmpty() {
30+
return this.array.length === 0; // return true if stack is empty
31+
}
32+
33+
printStack() {
34+
let data = "";
35+
for (let i = 0; i < this.array.length; i++)
36+
data += this.array[i] + " ";
37+
return data;
38+
}
39+
40+
}
41+
42+
function isBalancedParentheses(parentheses) {
43+
const myStack = new Stack();
44+
if(parentheses.length === 0) return true;
45+
if(parentheses.length === 1 || parentheses.length % 2 !== 0) return false;
46+
47+
for(const ch of parentheses) {
48+
switch(ch) {
49+
case '{':
50+
case '(':
51+
case '<':
52+
case '[':
53+
myStack.push(ch);
54+
break;
55+
case '}':
56+
if(myStack.peek() === '{') {
57+
myStack.pop();
58+
} else {
59+
return false;
60+
}
61+
break;
62+
case ')':
63+
if(myStack.peek() === '(') {
64+
myStack.pop();
65+
} else {
66+
return false;
67+
}
68+
break;
69+
case '>':
70+
if(myStack.peek() === '<') {
71+
myStack.pop();
72+
} else {
73+
return false;
74+
}
75+
break;
76+
case ']':
77+
if(myStack.peek() === '[') {
78+
myStack.pop();
79+
} else {
80+
return false;
81+
}
82+
break;
83+
}
84+
}
85+
if(myStack.isEmpty()) return true;
86+
}
87+
88+
89+
function useStack() {
90+
let myStack = new MyStack();
91+
92+
console.log(myStack.isEmpty()); // false
93+
console.log(myStack.pop()); // Underflow
94+
95+
myStack.push(1);
96+
myStack.push(2);
97+
myStack.push(3);
98+
99+
console.log(myStack.printStack()); // 1 2 3
100+
console.log(myStack.peek()); // 3
101+
102+
console.log(myStack.pop()); // 3
103+
104+
console.log(myStack.printStack()); // 1 2
105+
}
106+
107+
useStack();
108+
109+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
class Stack {
2+
3+
constructor() {
4+
this.array = []; // Array is used to implement stack
5+
}
6+
7+
// List of main functions of stack data structure
8+
9+
push(value) {
10+
// push an element into the array
11+
this.array.push(value);
12+
}
13+
14+
pop() {
15+
// Return null if stack is empty
16+
if (this.isEmpty()) {
17+
return null;
18+
}
19+
20+
return this.array.pop(); // return top most element from the stack and removes the same element
21+
}
22+
23+
peek() {
24+
// Return null if stack is empty
25+
if (this.isEmpty()) {
26+
return null;
27+
}
28+
29+
return this.array[this.array.length - 1]; // return top most element from the stack without removing the element
30+
}
31+
32+
// List of helper functions
33+
34+
isEmpty() {
35+
return this.array.length === 0; // return true if stack is empty
36+
}
37+
38+
size() {
39+
return this.array.length;
40+
}
41+
42+
printStack() {
43+
let data = "";
44+
for (let i = 0; i < this.array.length; i++)
45+
data += this.array[i] + " ";
46+
return data;
47+
}
48+
49+
}
50+
51+
function sortStack(stack) {
52+
if(stack.size() < 1) return;
53+
let tempStack = new Stack();
54+
while(stack.size() > 0) {
55+
let temp = stack.pop();
56+
while(tempStack.size() > 0 && tempStack.peek() < temp) {
57+
let top = tempStack.pop();
58+
stack.push(top);
59+
}
60+
tempStack.push(temp);
61+
}
62+
stack.array = tempStack.array;
63+
}
64+
65+
66+
let myStack = new Stack();
67+
68+
myStack.push(4);
69+
myStack.push(2);
70+
myStack.push(3);
71+
myStack.push(5);
72+
myStack.push(1);
73+
74+
console.log(myStack.printStack()); // 4 2 3 5 1
75+
sortStack(myStack);
76+
console.log(myStack.printStack()); // 5 4 3 2 1
77+
78+
79+

0 commit comments

Comments
 (0)