-
-
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.
Sorry, I made direct commit to my main branch, but no worries. I have…
… deleted it and than made another branch and made the commit. I have added 2 programs on Stack implementation using queue.
- Loading branch information
1 parent
60d6dd2
commit 482c638
Showing
2 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
1]. DSA/1]. Data Structures/09]. Stack/C++/_03)_stack_using_2_queue_costly_pop_approach.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,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; | ||
} |
67 changes: 67 additions & 0 deletions
67
1]. DSA/1]. Data Structures/09]. Stack/C++/_04)_stack_using_2_queue_costly_push_approach.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,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; | ||
} |