-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2bda863
commit 2a689fe
Showing
3 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
1]. DSA/1]. Data Structures/09]. Stack/C++/_05)_basics_of_array_imple_of_stack.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <iostream> | ||
#include <math.h> | ||
#include <algorithm> | ||
#include <vector> | ||
#include <string> | ||
#include <string.h> | ||
#include <ctype.h> | ||
|
||
using namespace std; | ||
|
||
class stack{ | ||
int* arr; | ||
int top; | ||
|
||
public: | ||
stack(int n) | ||
{ | ||
arr = new int[n]; | ||
top= -1; | ||
} | ||
|
||
void push(int x,int n) | ||
{ | ||
if(top == n-1) | ||
{ | ||
cout<<"\nStack Overflow"; | ||
return; | ||
} | ||
|
||
top++; | ||
arr[top] = x; | ||
} | ||
|
||
void pop() | ||
{ | ||
if(top == -1) | ||
{ | ||
return; | ||
} | ||
top--; | ||
} | ||
|
||
int peek() | ||
{ | ||
if(top == -1) | ||
{ | ||
cout<<"\nNo element "; | ||
return -1; | ||
} | ||
return arr[top]; | ||
} | ||
|
||
bool empty() | ||
{ | ||
return top==-1; | ||
} | ||
}; | ||
|
||
|
||
|
||
int main() | ||
{ | ||
int n,x; | ||
cin>>n; | ||
stack st(n); | ||
|
||
|
||
for(int i=0;i<n;i++) | ||
{ | ||
cin>>x; | ||
st.push(x,n); | ||
} | ||
|
||
for(int i=n;i>=0;i--) | ||
{ | ||
cout<<st.peek()<<" "; | ||
st.pop(); | ||
} | ||
|
||
return 0; | ||
} |
43 changes: 43 additions & 0 deletions
43
1]. DSA/1]. Data Structures/09]. Stack/C++/_06)_reverse_sentence_using_stack.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <iostream> | ||
#include <math.h> | ||
#include <algorithm> | ||
#include <vector> | ||
#include <string> | ||
#include <string.h> | ||
#include <stack> | ||
#include <ctype.h> | ||
|
||
using namespace std; | ||
|
||
void reversestring(string s) | ||
{ | ||
stack<string> st; | ||
|
||
for(int i=0;i<s.length();i++) | ||
{ | ||
string word=""; | ||
while(s[i]!=' ' && i<s.length()) | ||
{ | ||
word += s[i]; | ||
i++; | ||
} | ||
st.push(word); | ||
} | ||
|
||
while(!st.empty()) | ||
{ | ||
cout<<st.top()<<" "; | ||
st.pop(); | ||
} | ||
cout<<endl; | ||
} | ||
|
||
int main() | ||
{ | ||
string s; | ||
cin.ignore(); | ||
getline(cin,s); | ||
reversestring(s); | ||
|
||
return 0; | ||
} |
59 changes: 59 additions & 0 deletions
59
1]. DSA/1]. Data Structures/09]. Stack/C++/_07)_reverse_stack.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <iostream> | ||
#include <math.h> | ||
#include <algorithm> | ||
#include <vector> | ||
#include <string> | ||
#include <string.h> | ||
#include <stack> | ||
#include <ctype.h> | ||
|
||
using namespace std; | ||
|
||
void insertatbottom (stack<int> &st, int ele) | ||
{ | ||
if(st.empty()) | ||
{ | ||
st.push(ele); | ||
return; | ||
} | ||
int topele = st.top(); | ||
st.pop(); | ||
insertatbottom(st,ele); | ||
|
||
st.push(topele); | ||
} | ||
|
||
void reversestack(stack<int> &st) | ||
{ | ||
if(st.empty()) | ||
{ | ||
return; | ||
} | ||
|
||
int ele = st.top(); | ||
st.pop(); | ||
reversestack(st); | ||
insertatbottom(st,ele); | ||
} | ||
|
||
int main() | ||
{ | ||
stack<int> st; | ||
int n,e; | ||
cin>>n; | ||
for(int i=0;i<n;i++) | ||
{ | ||
cin>>e; | ||
st.push(e); | ||
} | ||
|
||
reversestack(st); | ||
|
||
while(!st.empty()) | ||
{ | ||
cout<<st.top()<<" "; | ||
st.pop(); | ||
} | ||
|
||
return 0; | ||
} |