MCPcopy Create free account
hub / github.com/Lakhankumawat/LearnCPP / queue

Class queue

Q-Queue/QueueUsingStacks.cpp:8–44  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6
7
8class queue{
9 stack<int> s1;
10 stack<int> s2;
11public:
12 //SIMPLY PUSH THE ELEMENT IN STACK1
13 void enqueue(int x){
14 s1.push(x);
15 }
16
17 int dequeue(){
18 if(s1.empty() && s2.empty()){
19 cout << "Error" << endl;
20 exit(0);
21 }
22 //REVERESE STACK1 BY USING STACK2
23 if(s2.empty()){
24 while(!s1.empty()){
25 s2.push(s1.top());
26 s1.pop();
27 }
28 }
29 //THEN SIMPLY POPPING THE FIRST ELEMENT OF STACK2, i.e. THE LAST ELEMENT OF STACK1
30 int topval = s2.top();
31 s2.pop();
32
33 return topval;
34 }
35
36 // RETURNS TRUE IF BOTH STACKS ARE EMPTY
37 bool empty(){
38 if(s1.empty() && s2.empty()){
39 return true;
40 }
41 else
42 return false;
43 }
44};
45
46/*
47DRIVER PROGRAM

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected