-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
51 lines (50 loc) · 864 Bytes
/
queue.c
File metadata and controls
51 lines (50 loc) · 864 Bytes
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
#include<stdio.h>
#include<stdlib.h>
typedef struct queue
{
int size;
int front,rear;
int *a;
}queue;
void enqueue(queue *q,int x)
{
if(q->rear==q->size-1)
printf("queue full\n");
else
{
q->rear++;
q->a[q->rear]=x;
}
}
int dequeue(queue *q)
{
int x=-1;
if(q->rear==q->front)//queue is empty.
return x;
else
{
q->front++;
x=q->a[q->front];
return x;
}
}
void display(queue q)
{
for(int i=q.front+1;i<=q.rear;i++)
{
printf("%d ",q.a[i]);
}
printf("\n");
}
void main()
{
queue q;
printf("Enter the size\n");
scanf("%d",&q.size);
q.front=q.rear=-1;
q.a=(int*)malloc(sizeof(int)*q.size);
enqueue(&q,4);
enqueue(&q,7);
printf("%d\n",dequeue(&q));
display(q);
}