-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quearray.c
77 lines (72 loc) · 1.06 KB
/
Quearray.c
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
#include<stdio.h>
#include<stdlib.h>
typedef struct queue{
int size;
int *arr;
int f;
int r;
}Q;
int isfull(Q*q){
if(q->r==q->size-1)
return 1;
return 0;
}
int isempty(Q*q){
if(q->r==q->f)
return 1;
return 0;
}
void enqueue(Q*q){
if(isfull(q)){
printf("Over Flow\n");
return ;
}
q->r++;
scanf("%d",&q->arr[q->r]);
if(q->f==-1)
q->f=q->r;
return;
}
int dequeue(Q*q){
if(isempty(q)){
int x=q->arr[q->f];
q->f++;
printf("Under Flow\n");
return x;
}
int x=q->arr[q->f];
q->f++;
return x;
}
void print(Q*q){
int f=q->f;
int r=q->r;
printf("From Front :\n");
for(int i=f;i<=r;i++)
printf("%d\n",q->arr[i]);
printf("From Rear :\n");
for(int i=r;i>=f;i--)
printf("%d\n",q->arr[i]);
}
int main(){
Q*q=(Q*)malloc(sizeof(Q));
printf("Enter size:\n");
scanf("%d",&q->size);
q->arr=(int*)malloc(sizeof(int)*q->size);
q->f=-1;
q->r=-1;
printf("Enter data:\n");
for(int i=0;i<=q->size-1;i++)
enqueue(q);
print(q);
dequeue(q);
print(q);
dequeue(q);
dequeue(q);
dequeue(q);
dequeue(q);
print(q);
printf("Re-Enter data:\n");
for(int i=0;i<=q->size-1;i++)
enqueue(q);
}