forked from BhaskarJoshi-01/HactoberFest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_array.cpp
72 lines (64 loc) · 1.22 KB
/
queue_array.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
//implementing a queue using a array structure
#include <iostream>
#include<iomanip>
using namespace std;
struct queue{
int size;
int f;
int b;
int * arr;
};
int isFull(struct queue *q){
if(q->b == q->size-1){
return 1;
}
else{return 0;}
}
int isEmpty(struct queue *q){
if(q->f ==q->b){
return 1;
}
else{return 0;}
}
void enqueue(struct queue *q, int val){
if(isFull(q)){
cout<<"Queue is full"<<endl;
}
else{
q->b=q->b + 1;
q->arr[q->b] = val;
}
}
int dequeue(struct queue *q){
if(isEmpty(q)){
cout<<"Queue is empty"<<endl;
return -1;
}
else{
q->f=q->f +1;
int val=q->arr[q->f];
return val;
}
}
void queuetraversal(struct queue* q){
cout<<"Traversing the queue elements:-- "<<endl;
for(int i=q->f+1;i<=q->b;i++){
cout<<q->arr[i]<<endl;
}
}
int main(){
struct queue q;
cout<<"Enter size of queue"<<endl;
cin>>q.size;
q.f=-1;
q.b=-1;
q.arr = new int[q.size];
enqueue(&q,4);
enqueue(&q,5);
enqueue(&q,8); //apply enqueue and dequeue operations
dequeue(&q);
dequeue(&q);
dequeue(&q);
dequeue(&q);
queuetraversal(&q);
}