-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq15)alloperationsofqueueusingarray.c
More file actions
119 lines (84 loc) · 1.92 KB
/
q15)alloperationsofqueueusingarray.c
File metadata and controls
119 lines (84 loc) · 1.92 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
// Created by mr_easy on 27/12/18.
// q15)implement all operations of queue ds using array,LL
//1)enqueue,dequeue
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
struct queue{
int front,rear, size,capacity;
int *arr;
};
struct queue q;
//initialization of structure variable
//using create function of structure
struct queue *createq(int capacity)
{
struct queue *q=(struct queue*)malloc(sizeof(struct queue));
q->capacity=capacity;
q->front=0;
q->rear=capacity-1;
q->size=0;
q->arr=(int *)malloc((q->capacity)*(sizeof(int)));
return q;
}
//to check whether queue is full or not
int isfull(struct queue *q)
{
return (q->size==q->capacity);
}
//to check whether queue is empty or not
int isempty(struct queue *q)
{
return(q->size==0);
}
//enqueue operations
void enqueue(struct queue *q,int item)
{
if(isfull(q))
return;
q->rear=(q->rear+1)%(q->capacity); //to make the circular array
q->arr[q->rear]=item;
q->size=q->size+1;
printf("%d\t",item);
}
//to get front of the element
int front(struct queue *q)
{
//check whether it is empty or not
if(isempty(q))
return INT_MIN;
return q->arr[q->front];
}
//to get the rear of the element
int rear(struct queue *q)
{
if(isempty(q))
return INT_MIN;
return q->arr[q->rear];
}
//to implement the dequeue operations
int dequeue(struct queue *q)
{
int i=q->arr[q->front];
q->front=(q->front+1)%(q->capacity);
q->size=q->size-1;
return i;
}
int main()
{
struct queue *q=createq(1000);
enqueue(q,2);
enqueue(q,3);
enqueue(q,5);
enqueue(q,1);
enqueue(q,7);
printf("d=%d\n",dequeue(q));
//to print the content of the queue
for(int i=q->front;i<=q->size;i++)
{
printf("%d\t",q->arr[i]);
}
printf("front element is:%d",q->arr[q->front]);
printf("rear element is:%d",q->arr[q->rear]);
return 0;
}