-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue(Dynamic array).c
105 lines (93 loc) · 1.86 KB
/
Queue(Dynamic array).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
#include<stdio.h>
#include<stdlib.h>
typedef struct Queue
{
int front,rear;
int *arr;
} queue;
queue* createQueue(int cap)
{
queue *Q;
Q=(queue*)malloc(sizeof(queue));
Q->front=0;
Q->rear=0;
Q->arr=malloc(cap*sizeof(int));
return Q;
}
void insert(queue *Q,int cap)
{
int item;
if(cap==Q->rear)
printf("Queue is Full\n");
else{
printf("Enter the item to insert\n");
scanf("%d",&item);
Q->arr[Q->rear]=item;
Q->rear++;
}
}
int delete(queue *Q)
{
int item;
if(Q->front==Q->rear)
{
printf("Queue is Empty\n");
return 0;
}
else
{
item=Q->arr[Q->front];
for(int i=0;i<(Q->rear)-1;i++)
Q->arr[i]=Q->arr[i+1];
Q->rear--;
return item;
}
}
void display(queue *Q)
{
int i;
if(Q->front==Q->rear)
{
printf("Queue is Empty\n");
}
else
{
for(i=0;i<Q->rear;i++)
printf("%d\n",Q->arr[i]);
}
}
void main()
{
int choice,del_item,cap;
queue *Q;
printf("Enter the capacity of Queue\n");
scanf("%d",&cap);
Q=createQueue(cap);
while(1)
{
printf("1.Insert in Queue\n");
printf("2.Delete from Queue\n");
printf("3.Display Queue\n");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert(Q,cap);
break;
case 2:
del_item=delete(Q);
printf("Deleted item is %d\n",del_item);
break;
case 3:
display(Q);
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice");
break;
}
}
}