-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueue.c
More file actions
120 lines (98 loc) · 1.98 KB
/
Queue.c
File metadata and controls
120 lines (98 loc) · 1.98 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Implimantation of Queue :- woeks on the principal of FIFO :- first in first out
#include <stdio.h>
#include <stdlib.h>
struct queue
{
int f, r, size;
int *arr;
};
// to insert values in queue;
void enqueue(struct queue *q, int a)
{
if (q->r == q->size - 1)
{
printf("Queue is full");
}
else
{
q->arr[++q->r] = a;
}
}
// to remove values from queue;
int dequeue(struct queue *q)
{
if (q->f == q->r)
{
printf("Queue is empty");
return -1;
}
else
{
return q->arr[++q->f];
}
}
void traversal(struct queue *q)
{
if (q->f == q->r)
{
printf("Queue is empty");
}
else
{
int i = q->f + 1;
for (i; i <= q->r; i++)
{
printf("%d ", q->arr[i]);
}
}
}
int main()
{
char c;
int x;
struct queue *q = (struct queue *)malloc(sizeof(struct queue));
printf("Enter size of queue : ");
scanf("%d", &q->size);
q->f = -1;
q->r = -1;
q->arr = (int *)malloc(q->size * sizeof(int));
printf("Enter element of queue : ");
for (int j = 0; j < q->size; j++)
{
int u;
scanf("%d", &u);
enqueue(q, u);
}
printf("queue is : ");
traversal(q);
// taking user input whether we wants to delete element or not;
printf("\nwant to delete elements : ");
getchar(); //to consume new line character;
scanf("%c", &c);
if (c == 'y' || c == 'Y')
{
printf("how many elements : ");
scanf("%d", &x);
printf("Dequeued elements : ");
for (int i = 0; i < x; i++)
{
int e = dequeue(q);
if(e == -1){
break;
}
printf("%d ", e);
}
}
else
{
free(q->arr);
free(q);
return 0;
}
// printing queue after removal of elements;
printf("\nModified queue is : ");
traversal(q);
free(q->arr);
free(q);
return 0;
}