Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
amandewatnitrr authored Jun 26, 2021
1 parent 2bda863 commit 2a689fe
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
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;
}
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 1]. DSA/1]. Data Structures/09]. Stack/C++/_07)_reverse_stack.cpp
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;
}

0 comments on commit 2a689fe

Please sign in to comment.