forked from santoshvijapure/DS_with_hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.Deque.c
106 lines (104 loc) · 1.95 KB
/
9.Deque.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<stdio.h>
int size;
struct queue
{
int a[30];
int front;
int rear;
};
void enqueue(struct queue *p)
{ int item;
printf("Enter the item:");
scanf("%d",&item);
if(p->front==-1)
{
p->rear=size-1;
p->front=size-1;
p->a[p->front]=item;
}
else
{
int ahead;
if(p->front==0)
{
ahead=size-1;
}
else
{
ahead=p->front-1;
p->front=ahead;
p->a[p->front]=item;
}
}
}
void dequeue(struct queue *p)
{
if(p->front==-1)
{
printf("Queue is empty");
}
else
{
int item;
item=p->a[p->rear];
printf("Deleted item=%d",item);
if(p->front==p->rear)
{
p->front=-1;
p->rear=-1;
}
else
{
p->rear=p->rear-1;
}
}
}
void display(struct queue *p)
{
int i;
printf("Deque");
if(p->front>p->rear)
{
for(i=p->front;i<=size-1;i++)
printf("%d",p->a[i]);
for(i=0;i<p->rear;i++)
printf("%d",p->a[i]);
}
else
{
for(i=p->front; i<=p->rear; i++)
{
printf("%d ",p->a[i]);
}
printf("\n");
}
}
void main()
{
int ch,t=1;
struct queue q;
q.front=-1;
q.rear=-1;
printf("Enter the size:");
scanf("%d",&size);
while(t==1)
{
printf("Enter the choice\n 1.ENQUEUE\n 2.DEQUEUE\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue(&q);
display(&q);
break;
case 2:
dequeue(&q);
display(&q);
break;
default:
printf("Invalid choice\n");
}
printf("\nDo you want to continue \n1.yes 2.no\n");
scanf("%d",&t);
}
}