-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue_using_stack.cpp
46 lines (32 loc) · 972 Bytes
/
queue_using_stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
/*Queue structure for making stack and implementing queue*/
struct Queue {
stack<int> stk; //Initializing stack stk
//addToQueue function is used to push elements to the stack
void addToQueue(int i) {
stk.push(i);
}
//deleteFromQueue is used to pop elements from stack
int deleteFromQueue() {
//If there is no element in the stack
if (stk.empty()) {
cout << "Queue is empty";
exit(0);
}
int i = stk.top(); //Intitialize i with topmost element of stack
stk.pop(); //Pop the element from the stack
//If it was first element of stack, return it
if (stk.empty())
return i;
int item = deleteFromQueue(); //Call deleteFromQueue recursively until first element is recieved
stk.push(i); //Push the popped element back into stack
return item; //Return the item
}
};
int main() {
Queue q;
q.addToQueue(2); q.addToQueue(4); q.addToQueue(7);
cout << q.deleteFromQueue();
return 0;
}