Skip to content

Create Implement Queue from Stack #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions STL/Queue/Implement Queue from Stack
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Popular Interview Question: How to implement Queue from Stack

// SOLUTION

Queue follows the principle of FIFO, whereas Stack follows the principle of LIFO
Hence because of this fundamental difference, we can easily pop an element from stack, but pushing it can get tricky.

// BASIC IDEA


POP FROM QUEUE: Pop the the top element of stack
st.pop();

PUSH INTO QUEUE: Push the element at the bottom of the stack(not at the top)
void push_bottom(stack<int> &s, int d){
//base case
if(s.empty()){
s.push(d);
return;
}

//recursive case
int top_element = s.top();
s.pop();
push_bottom(s,d);
s.push(top_element);
}

// T(n) = T(n-1) + c => O(n)

// COMPLETE IMPLEMENTATION OF QUEUE USING A SINGLE STACK
#include<iostream>
#include<stack>
using namespace std;

void push_bottom(stack<int> &s, int d){ // T(n) = T(n-1) + c => O(n)
//base case
if(s.empty()){
s.push(d);
return;
}

//recursive case
int top_element = s.top();
s.pop();
push_bottom(s,d);
s.push(top_element);
}

class Queue{
public:
stack<int> s1;


void push(int d){ // O(n) { WE HAD TO COMPROMISE ON PUSH }
push_bottom(s1,d);
}
void pop(){ // O(1)
s1.pop();
}
int front(){ // O(1)
return s1.top();
}
bool empty(){
return !s1.size();
}
int size(){
return s1.size();
}
};

int main(){
Queue q;
for(int i=0;i<5;i++) q.push(i+1);

cout<<"Q: ";
while(!q.empty()){
cout<<q.front()<<" ";
q.pop();
}cout<<endl;

return 0;
}