-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueusingarray.cpp
More file actions
76 lines (70 loc) · 1.51 KB
/
queueusingarray.cpp
File metadata and controls
76 lines (70 loc) · 1.51 KB
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
class Queue {
int *arr;
int qfront,rear,size;
public:
Queue() {
// Implement the Constructor
size=100001;
qfront=0;
rear=0;
arr=new int[size];
}
/*----------------- Public Functions of Queue -----------------*/
bool isEmpty() {
// Implement the isEmpty() function
if(qfront==rear) //no ele inserted
{
return true;
}
else
{
return false;
}
}
void enqueue(int data) {
// Implement the enqueue() function
//push/insert
//check for full queue
if(rear==size)
cout<<"Queue is full"<<endl;
else
{
//push ele
arr[rear]=data;
rear++;
}
}
int dequeue() {
// Implement the dequeue() function
//pop ele
//check for empty
if(qfront==rear)
{
return -1;
}
else
{
int ans=arr[qfront]; //to print ans;
arr[qfront]=-1;
qfront++; //aage se delete hoga ele
//arises
if(qfront==rear)
{
qfront=0;
rear=0;
}
return ans;
}
}
int front() {
// Implement the front() function
//to print front ele always
if(qfront==rear)
return -1;
else
{
return arr[qfront];
}
}
//all operations take o(1) tc.
};