0% found this document useful (0 votes)
37 views38 pages

Data Structures: Radhamadhab Dalai Asst. Professor CSE BIT Mesra, Ranchi

The document discusses circular queues and their implementation using arrays. It defines a circular queue struct with fields for a data array, front and rear indexes, and max size. It shows examples of initializing a circular queue and enqueue and dequeue operations, with the rear index increasing and front decreasing modulo the max size to allow wrapping as elements are added and removed.

Uploaded by

Aditya Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views38 pages

Data Structures: Radhamadhab Dalai Asst. Professor CSE BIT Mesra, Ranchi

The document discusses circular queues and their implementation using arrays. It defines a circular queue struct with fields for a data array, front and rear indexes, and max size. It shows examples of initializing a circular queue and enqueue and dequeue operations, with the rear index increasing and front decreasing modulo the max size to allow wrapping as elements are added and removed.

Uploaded by

Aditya Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Data Structures

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

A next B next C next

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));

• q-> front = q-> rear=NULL;


• struct node *temp = NULL;
• temp = (struct node *)malloc(sizeof( struct node));
• temp - > data = key;
• If(!NotEmpty(q)){
q->front =temp;
q->rear = temp;
temp -> next =NULL;
}

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

You might also like