From 93db8eb3079ac9fedca5b7415d951afca71cb2bb Mon Sep 17 00:00:00 2001 From: Happy Date: Wed, 26 Jan 2022 11:59:11 +0530 Subject: [PATCH] Add QueueUsingStack --- Stack/QueueusingStack.cpp | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Stack/QueueusingStack.cpp diff --git a/Stack/QueueusingStack.cpp b/Stack/QueueusingStack.cpp new file mode 100644 index 0000000..b6f09f6 --- /dev/null +++ b/Stack/QueueusingStack.cpp @@ -0,0 +1,77 @@ +#include +#include +using namespace std; + +template +class Queue +{ + stack s1, s2; + +public: + void push(T item) + { + s1.push(item); + } + void pop() + { + if (!s1.empty()) + { + while (s1.size() >= 1) + { + T item = s1.top(); + s2.push(item); + s1.pop(); + } + } + + s2.pop(); + } + T front() + { + if (!s1.empty()) + { + while (s1.size() >= 1) + { + T item = s1.top(); + s2.push(item); + s1.pop(); + } + } + + T element = s2.top(); + return element; + } + int size() + { + return s1.size() + s2.size(); + } + bool empty() + { + return size() == 0; + } +}; + +void display(Queue& q){ + while (!q.empty()) + { + cout << q.front() << " "; + q.pop(); + } + cout << endl; +} + +int main() +{ + Queue q; + q.push(1); + q.push(2); + q.push(3); + q.push(4); + + display(q); + + q.push(5); + q.push(6); + + display(q); +} \ No newline at end of file