-
Notifications
You must be signed in to change notification settings - Fork 0
/
queues.cpp
89 lines (72 loc) · 1.53 KB
/
queues.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
#include<bits/stdc++.h>
using namespace std;
class Queue{
public:
int fron,rear,siz;
unsigned capacity;
int *arr;
};
Queue* createq(unsigned capacity){
Queue *queu = new Queue();
queu->capacity = capacity;
queu->siz = 0;
queu->fron = 0;
queu->rear = capacity-1;
queu->arr = new int[queu->capacity];
return queu;
}
bool isfull(Queue *q){
if(q->siz==q->capacity){
return true;
}else{
return false;
}
}
bool isempty(Queue *q){
if(q->siz==0){
return true;
}else{
return false;
}
}
void enqueue(Queue *q,int v){
if(isfull(q)){
return;
}
q->rear = (q->rear+1)%q->capacity;
q->arr[q->rear]=v;
q->siz=q->siz+1;
cout<<v<<" enqueued in the queue\n";
}
int dequeue(Queue *q){
if(isempty(q))
return INT_MIN;
int item = q->arr[q->fron];
q->fron = (q->fron+1) % q->capacity;
q->siz=q->siz-1;
return item;
}
int frontitem(Queue *q){
if (isempty(q))
return INT_MIN;
return q->arr[q->fron];
}
int rearitem(Queue *q){
if (isempty(q))
return INT_MIN;
return q->arr[q->rear];
}
int main(){
Queue* q=createq(100);
enqueue(q,23);
enqueue(q,25);
enqueue(q,36);
enqueue(q,41);
enqueue(q,99);
cout<<"\n"<<frontitem(q)<<" front item before op\n";
cout<<rearitem(q)<<" rear item before op\n\n";
cout<<dequeue(q)<<" dequed from the queue\n\n";
cout<<frontitem(q)<<" front item after op\n";
cout<<rearitem(q)<<" rear item after op\n";
return 0;
}