Skip to content

Commit d541db0

Browse files
Stack DataStructure added
1 parent c4a0ee5 commit d541db0

10 files changed

+584
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
5+
void solve(stack<int>&inputStack, int count, int size){
6+
7+
//base case
8+
if(count==size/2){
9+
inputStack.pop();
10+
return;
11+
}
12+
13+
int num = inputStack.top(); //store the top element
14+
inputStack.pop(); //pop that element
15+
16+
//recursive call
17+
solve(inputStack,count+1, size);
18+
19+
inputStack.push(num); // push back the top elements which pop in the process
20+
}
21+
22+
23+
void deleteMiddle(stack<int>&inputStack, int N){
24+
25+
int count =0;
26+
solve(inputStack,count,N);
27+
28+
}
29+
30+
int main(){
31+
32+
stack<int> inputStack;
33+
34+
inputStack.push(5);
35+
inputStack.push(9);
36+
inputStack.push(12);
37+
inputStack.push(8);
38+
inputStack.push(4);
39+
40+
int N = inputStack.size();
41+
42+
// for(int i=0; i<N; i++){
43+
// cout<<inputStack.top()<<" ";
44+
// inputStack.pop();
45+
// }
46+
// cout<<endl;
47+
48+
deleteMiddle(inputStack,N);
49+
50+
for(int i=0; i<N; i++){
51+
cout<<inputStack.top()<<" ";
52+
inputStack.pop();
53+
}
54+
55+
return 0;
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
int findMinimumCost(string str) {
5+
6+
//odd condition
7+
if(str.length()%2 == 1) {
8+
return -1;
9+
}
10+
11+
stack<char> s;
12+
for(int i=0; i<str.length(); i++) {
13+
char ch = str[i];
14+
15+
if(ch == '{')
16+
s.push(ch);
17+
else
18+
{
19+
//ch is closed brace
20+
if(!s.empty() && s.top() == '{') {
21+
s.pop();
22+
}
23+
else
24+
{
25+
s.push(ch);
26+
}
27+
}
28+
}
29+
30+
//stack contains invalid expression
31+
int a = 0, b = 0;
32+
while(!s.empty()) {
33+
if(s.top() == '{') {
34+
b++;
35+
}
36+
else
37+
{
38+
a++;
39+
}
40+
s.pop();
41+
}
42+
43+
int ans = (a+1)/2 + (b+1)/2;
44+
return ans;
45+
46+
47+
}
48+
49+
50+
int main(){
51+
52+
53+
string s = "{{}{}}";
54+
55+
cout<<findMinimumCost(s)<<endl;
56+
57+
return 0;
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include<stack>
3+
#include<vector>
4+
using namespace std;
5+
6+
vector<int> nextSmallerElement(vector<int>& arr, int n){
7+
stack<int>s;
8+
9+
s.push(-1);
10+
vector<int>ans(n);
11+
12+
for(int i=n-1; i>=0; i--){
13+
int cur = arr[i];
14+
15+
while(s.top() >= cur){
16+
s.pop();
17+
}
18+
//ans is stack ka top
19+
ans[i]=s.top();
20+
s.push(cur);
21+
}
22+
return ans;
23+
}
24+
25+
int main(){
26+
27+
int n= 4;
28+
vector<int> arr= {2,1,4,3};
29+
30+
vector<int>ans =nextSmallerElement(arr,n);
31+
32+
for(int i=0; i<n; i++){
33+
cout<<ans[i]<<" ";
34+
}
35+
36+
return 0;
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
5+
bool findRedundantBrackets(string &s)
6+
{
7+
stack<char>st;
8+
9+
for(int i=0; i<s.length(); i++){
10+
char ch = s[i];
11+
12+
if(ch=='(' || ch=='+' || ch=='-' || ch=='*' || ch=='/'){
13+
st.push(ch);
14+
}
15+
16+
else{
17+
18+
if(ch==')'){
19+
bool isRedundant = true;
20+
21+
while(st.top()!='('){
22+
char top = st.top();
23+
24+
if(top=='+' || top=='-' || top=='*' || top=='/'){
25+
isRedundant=false;
26+
st.pop();
27+
}
28+
}
29+
30+
if(isRedundant == true){
31+
return true;
32+
}
33+
34+
st.pop();
35+
}
36+
}
37+
}
38+
39+
return false;
40+
41+
42+
}
43+
44+
int main(){
45+
46+
string s = "(a+c*b)+(c))";
47+
48+
cout<<findRedundantBrackets(s)<<endl;
49+
50+
return 0;
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
5+
6+
7+
void insertAtBottom(stack<int> &stack, int x){
8+
if(stack.empty()){
9+
stack.push(x);
10+
return;
11+
}
12+
13+
int num = stack.top();
14+
stack.pop();
15+
16+
insertAtBottom(stack, x);
17+
18+
stack.push(num);
19+
}
20+
21+
void reverseStack(stack<int> &stack) {
22+
23+
//base case
24+
if(stack.empty()){
25+
return;
26+
}
27+
28+
int num = stack.top();
29+
stack.pop();
30+
31+
reverseStack(stack);
32+
33+
insertAtBottom(stack,num);
34+
}
35+
36+
int main(){
37+
38+
stack<int> inputStack;
39+
40+
inputStack.push(5);
41+
inputStack.push(9);
42+
inputStack.push(12);
43+
inputStack.push(8);
44+
inputStack.push(4);
45+
46+
// for(int i=0; i<5; i++){
47+
// cout<<inputStack.top()<<" ";
48+
// inputStack.pop();
49+
// }
50+
51+
reverseStack(inputStack);
52+
53+
for(int i=0; i<5; i++){
54+
cout<<inputStack.top()<<" ";
55+
inputStack.pop();
56+
}
57+
58+
59+
return 0;
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<iostream>
2+
#include<stack>
3+
4+
using namespace std;
5+
6+
int main(){
7+
8+
string str = "life";
9+
10+
cout<<str<<endl;
11+
12+
stack <char> s;
13+
14+
for(int i=0; i<str.length(); i++){
15+
char ch = str[i];
16+
s.push(ch);
17+
}
18+
19+
string ans ="";
20+
21+
while(!s.empty()){
22+
char c = s.top();
23+
ans.push_back(c);
24+
s.pop();
25+
}
26+
27+
cout<<ans<<endl;
28+
29+
return 0;
30+
}

data_structure/Stack/Sort_stack.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
5+
void sort(stack<int> &stack, int x){
6+
7+
//base case
8+
if(stack.empty() || !stack.empty() && stack.top() < x ){
9+
stack.push(x);
10+
return;
11+
}
12+
13+
int n = stack.top();
14+
stack.pop();
15+
16+
sort(stack,x);
17+
18+
stack.push(n);
19+
20+
}
21+
22+
void sortStack(stack<int> &stack)
23+
{
24+
//base case
25+
if(stack.empty()){
26+
return;
27+
}
28+
29+
int num = stack.top();
30+
stack.pop();
31+
32+
sortStack(stack);
33+
34+
sort(stack,num);
35+
36+
}
37+
38+
39+
int main(){
40+
41+
stack<int> inputStack;
42+
43+
inputStack.push(5);
44+
inputStack.push(9);
45+
inputStack.push(12);
46+
inputStack.push(8);
47+
inputStack.push(4);
48+
49+
sortStack(inputStack);
50+
51+
for(int i=0; i<5; i++){
52+
cout<<inputStack.top()<<" ";
53+
inputStack.pop();
54+
}
55+
56+
57+
return 0;
58+
59+
}

0 commit comments

Comments
 (0)