-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_queue.c
102 lines (96 loc) · 3.18 KB
/
circular_queue.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "circular_queue.h"
int insert(int filesize,int copies,char filename[20],struct Circular_Queue *CQ)
{
// Normal insertion logic of a circular queue
// if the front index is equal to rear index + 1 ( or ) front index is 0 and rear index is max
if((CQ->front == 0 && CQ->rear == CQ->MAX-1) || (CQ->front == CQ->rear+1)){
return -1;
}// At start both indexes will be -1 after inserting first element both will be 0
if(CQ->front == -1){
CQ->front = 0;
CQ->rear = 0;
}// If the element to be inserted is not the first element
else{
// If the rear index has reached max size it is changed to 0
if(CQ->rear == CQ->MAX-1)
CQ->rear = 0;
else
CQ->rear = CQ->rear+1;
}
// Insertion of elements to photocopy is made at the photocopy of rear index
CQ->cqueue_arr[CQ->rear].copies = copies ;
CQ->cqueue_arr[CQ->rear].filesize = filesize;
strcpy(CQ->cqueue_arr[CQ->rear].filename,filename);
printf("%s %d\n",CQ->cqueue_arr[CQ->rear].filename,CQ->rear);
return 0;
}
// The front index will be returned for reference
struct Photocopy* getFront(struct Circular_Queue *CQ)
{
printf("\n%d copies of %d pages of File : %s\n",CQ->cqueue_arr[CQ->front].copies,CQ->cqueue_arr[CQ->front].filesize,CQ->cqueue_arr[CQ->front].filename);
return &(CQ->cqueue_arr[CQ->front]);
};
// Function to delete front element in the queue
int deletion(struct Circular_Queue *CQ)
{
// deletion will not be done if the queue is empty
if(CQ->front == -1){
return -1;
}
printf("Photocopy is taken so file : %s is removed from queue\n",CQ->cqueue_arr[CQ->front].filename);
if(CQ->front == CQ->rear){
CQ->front = -1;
CQ->rear=-1;
}// if the rear index and front index is max size it will be set to 0 as it is circular else front will be incremented
else{
if(CQ->front == CQ->MAX-1)
CQ->front = 0;
else
CQ->front = CQ->front+1;
}
return 0;
}
// negative return value means queue is empty
// else it is not empty
int isEmpty(struct Circular_Queue *CQ){
if(CQ->front == -1){
return -1;
}else{
return 0;
}
}
// displaying all elements in circular queue
int displayall(struct Circular_Queue *CQ)
{
int front_pos = CQ->front,rear_pos = CQ->rear;
// checking whether queue is empty
if(CQ->front == -1)
{
return -1;
}
printf("Queue elements :\n");
// if front index is less than rear index
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("Queue[%d]:-\nCopies:%d\tPages:%d\tFilename:%s\n",front_pos,CQ->cqueue_arr[front_pos].copies,CQ->cqueue_arr[front_pos].filesize,CQ->cqueue_arr[front_pos].filename);
front_pos++;
}
else
{ //front index is not less than rear index so it is split as front index to max and 0 to rear index
while(front_pos <= CQ->MAX-1){
printf("Queue[%d]:-\nCopies:%d\tPages:%d\tFilename:%s\n",front_pos,CQ->cqueue_arr[front_pos].copies,CQ->cqueue_arr[front_pos].filesize,CQ->cqueue_arr[front_pos].filename);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos){
printf("Queue[%d]:-\nCopies:%d\tPages:%d\tFilename:%s\n",front_pos,CQ->cqueue_arr[front_pos].copies,CQ->cqueue_arr[front_pos].filesize,CQ->cqueue_arr[front_pos].filename);
front_pos++;
}
}
printf("\n");
return 0;
}