-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble_ended_queue.c
64 lines (64 loc) · 1.02 KB
/
double_ended_queue.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
#include<stdio.h>
#include<stdlib.h>
typedef struct queue
{
int front,rear,n;
int *a;
}queue;
void enqueue_r(queue *q,int x)
{
q->rear++;
q->a[q->rear]=x;
}
int dequeue_f(queue *q)
{
q->front++;
return q->a[q->front];
}
void enqueue_f(queue *q,int x)
{
if(q->front==-1)
{printf("cann't be inserted\n");
return;
}
else
{
q->a[q->front]=x;
q->front--;
}
}
int dequeue_r(queue *q)
{
int x;
if(q->rear==-1)
printf("can't be deleted\n");
else
{
x=q->a[q->rear];
q->rear--;
return x;
}
}
void display(queue q)
{
int i=0;
for(i=q.front+1;i<=q.rear;i++)
{
printf("%d ",q.a[i]);
}
}
int main()
{
queue q;
scanf("%d",&q.n);
q.a=(int *)malloc(sizeof(int)*q.n);
q.front=q.rear=-1;
enqueue_r(&q,5);
enqueue_r(&q,7);
enqueue_r(&q,3);
enqueue_r(&q,1);
dequeue_f(&q);
enqueue_f(&q,0);
dequeue_r(&q);
display(q);
}