Data Structures: Radhamadhab Dalai Asst. Professor CSE BIT Mesra, Ranchi
Data Structures: Radhamadhab Dalai Asst. Professor CSE BIT Mesra, Ranchi
Radhamadhab Dalai
Asst. Professor
CSE
BIT Mesra, Ranchi
08-09-2020 1
Circular Queue(CQ)
• CQ using array
12 Fixed array
19 7
front 28 6
18
14 rear
08-09-2020 2
Circular Queue
• struct q
{
int *data;
int front;
int rear;
int max;
};
08-09-2020 3
Circular Queue
• Initialization of circular q
• struct q *initialize(int size)
{
struct q *cq= (struct q)malloc(sizeof(struct q));
if(!cq)
return null;
else {
cq->max =size;
cq->data = malloc(size*sizeof(int));
cq->front = cq->rear =-1;
08-09-2020 4
Circular Queue
• front = -1; rear =-1;
rear
Fixed array
front
MAX = 14
08-09-2020 5
Circular Queue
• enqueue(18)
front rear
MAX = 14
18
08-09-2020 6
Circular Queue
• enqueue(21) rear
front MAX = 14
21
18
08-09-2020 7
Circular Queue
• enqueue(25)
front rear MAX = 14
21
18 25
08-09-2020 8
Circular Queue
• enqueue(11)
front rear MAX = 14
21
18 25
11
08-09-2020 9
Circular Queue
• enqueue(5)
front rear MAX = 14
21
18 25
11
08-09-2020 10
Circular Queue
• enqueue(5)
front MAX = 14
21
18 25
11
rear
5
08-09-2020 11
Circular Queue
• enqueue(20)
front MAX = 14
21
18 25
11
20
rear
08-09-2020 12
Circular Queue
• enqueue(4)
front MAX = 14
21
18 25
11
20
rear
08-09-2020 13
Circular Queue
• enqueue(20)
front MAX = 14
21
18 25
11
20
rear
08-09-2020 14
Circular Queue
• enqueue(4)
front MAX = 14
21
18 25
11
20
4 rear
08-09-2020 15
Circular Queue
• enqueue(27)
front MAX = 14
21
18 25
11
20
4
27
rear
08-09-2020 16
Circular Queue
• enqueue(32)
front MAX = 14
21
18 25
11
20
4
27
32
rear
08-09-2020 17
Circular Queue
• enqueue(12)
front MAX = 14
21
18 25
11
20
4
12 27
rear 32
08-09-2020 18
Circular Queue
• enqueue(40)
front MAX = 14
21
18 25
11
20
40 4
rear 27
12
32
08-09-2020 19
Circular Queue
• enqueue(26)
front MAX = 14
21
18 25
11
26
rear 20
40 4
12 27
32
08-09-2020 20
Circular Queue
• enqueue(17)
front MAX = 14
21
18 25
11
rear
17
5
26
20
40 4
12 27
32
08-09-2020 21
Circular Queue
• enqueue(21)
front
MAX = 14
rear 21
18 25
21
11
17
5
26
20
40 4
12 27
32
08-09-2020 22
Circular Queue
• enqueue(19)
front
MAX = 14
rear 21
18 25
21
11
17
5
26
20
40 4
12 27
32
08-09-2020 23
Circular Queue
• dequeue()
front
MAX = 14
rear 21
25
21
11
17
5
26
20
40 4
12 27
32
08-09-2020 24
Circular Queue
front
• dequeue()
MAX = 14
rear
25
21
11
17
5
26
20
40 4
12 27
32
08-09-2020 25
Circular Queue
rear MAX = 14
front 21
08-09-2020 26
Circular Queue
• int isEmptyQ(struct q *cq){
• If (cq->front ==-1)
return 1;
• }
08-09-2020 27
Circular Queue
• void enq(struct q *cq, int key)
{
if(isFullQ(cq))
{
printf(“No element can be inserted”) ;
//arrangeCQ(cq);
}
cq->rear = (cq->rear+1) % cq->max;
cq -> data[cq->rear] = key;
If(cq->front ==-1){
cq->front = cq->rear;
}
}
• int isFullQ(struct q *cq){
• If (cq->rear +1) % cq->max == q->front)
• return 1;
• }
08-09-2020 28
Circular Queue
• int deqeueCQ(struct q *cq){
int key =0;
if(isEmpty(cq)){
printf(“q is empty.”)
return 0;
}
else{
key = cq->data[cq->front];
if (cq->front == cq->rear){
cq->front = cq->rear =-1;
}
else{
cq->front = (cq->front) % cq->max;
}
}
return key;
}
08-09-2020 29
Questions
• Questions ?
08-09-2020 30
Circular Queue Using CSLL
• Circular Q using Circular Singly linked list.
head
front
08-09-2020 31
Circular Queue Using CSLL
• struct node
{
int data;
struct node * next;
};
• struct queue
{
struct node * front;
struct node *rear;
};
08-09-2020 32
Circular Queue Using CSLL
• int keys[]={9,14,62,12};
• struct queue *q;
• q = (struct node *)malloc(sizeof( struct queue));
08-09-2020 33
Circular Queue Using CSLL
• else
{
q->rear -> next = temp;
q->rear = temp;
temp -> next = NULL;
return q;
}
08-09-2020 34
Circular Queue Using CSLL
• int deqeueCQ(struct q *cq){
int key =0;
if(isEmpty(cq)){
printf(“q is empty.”)
return 0;
}
else{
key = cq->data[cq->front];
if (cq->front == cq->rear){
cq->front = cq->rear =-1;
}
else{
cq->front = (cq->front) % cq->max;
}
}
return key;
}
08-09-2020 35
Priority Queue
• struct pqueue
{
int data;
int priority;
};
• Int pq[MAX], front=-1, rear=-1,i,n;
08-09-2020 36
Priority Queue
• It is a queue with similar structure as normal
queue with priority as another field which
holds an integer value. Each element in the
queue has a priority value.
• Based on priority value the following
operations are done in similar ways as queue’s
operations
• Enqueue(), Dequeue()
08-09-2020 37
Questions
• Questions ?
08-09-2020 38