Skip to content

Commit

Permalink
Sorry, I made direct commit to my main branch, but no worries. I have…
Browse files Browse the repository at this point in the history
… deleted it and than made another branch and made the commit. I have added 2 programs on Stack implementation using queue.
  • Loading branch information
amandewatnitrr committed Jun 24, 2021
1 parent 60d6dd2 commit 482c638
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <bits/stdc++.h>
using namespace std;

class stackk
{
int n;
queue<int> q1;
queue<int> q2;

public:

stackk()
{
n = 0;
}

void push(int x)
{
q1.push(x);
n++;
}

void pop()
{
if(q1.empty())
{
cout<<"Empty Stack";
return;
}
while(q1.size()!=1)
{
q2.push(q1.front());
q1.pop();
}
q1.pop();
n--;
queue<int> temp = q1;
q1 = q2;
q2 = temp;
}

int top()
{
if(q1.empty())
{
cout<<"Empty Stack";
return -1;
}
while(q1.size()!=1)
{
q2.push(q1.front());
q1.pop();
}
int ans = q1.front();
q2.push(ans);
queue<int> temp = q1;
q1 = q2;
q2 = temp;
return ans;
}

int size(){return n;}

};

int main()
{
stackk s;
int n,x;
cin>>n;

for(int i=0;i<n;i++)
{
cin>>x;
s.push(x);
}

while(s.size()>0)
{
cout<<s.top()<<" ";
s.pop();
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <bits/stdc++.h>
using namespace std;

class stackk
{
int n;
queue<int> q1;
queue<int> q2;

public:

stackk()
{
n = 0;
}

void push(int x)
{
q2.push(x);
while(!q1.empty())
{
q2.push(q1.front());
q1.pop();
}
queue<int> temp = q1;
q1 = q2;
q2 = temp;
n++;
}

void pop()
{
if(q1.empty())
{
cout<<"Empty Stack";
return;
}
q1.pop();
n--;
}

int top(){ return q1.front();}

int size(){return n;}

};

int main()
{
stackk s;
int n,x;
cin>>n;

for(int i=0;i<n;i++)
{
cin>>x;
s.push(x);
}

while(s.size()>0)
{
cout<<s.top()<<" ";
s.pop();
}

return 0;
}

0 comments on commit 482c638

Please sign in to comment.