-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_ImplementQueueUsingArray.cpp
90 lines (76 loc) · 1.33 KB
/
GFG_ImplementQueueUsingArray.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
https://practice.geeksforgeeks.org/problems/implement-queue-using-array/1
Implement Queue using array
*/
#include<bits/stdc++.h>
using namespace std;
struct QueueNode
{
int data;
QueueNode *next;
};
class MyQueue {
private:
int arr[100005];
int front;
int rear;
public :
MyQueue(){front=0;rear=0;}
void push(int);
int pop();
};
int main()
{
int T;
cin>>T;
while(T--)
{
MyQueue *sq = new MyQueue();
int Q;
cin>>Q;
while(Q--){
int QueryType=0;
cin>>QueryType;
if(QueryType==1)
{
int a;
cin>>a;
sq->push(a);
}else if(QueryType==2){
cout<<sq->pop()<<" ";
}
}
cout<<endl;
}
}
// } Driver Code Ends
/*
The structure of the class is
class MyQueue {
private:
int arr[100005];
int front;
int rear;
public :
MyQueue(){front=0;rear=0;}
void push(int);
int pop();
};
*/
//Function to push an element x in a queue.
void MyQueue :: push(int x)
{
if(rear == 100005) {
cout<<"Queue Full.";
return;
}
arr[rear++] = x;
}
//Function to pop an element from queue and return that element.
int MyQueue :: pop()
{
// Your Code
if(rear == front)
return -1;
return arr[front++];
}