-
Notifications
You must be signed in to change notification settings - Fork 1
/
Queue.c
200 lines (184 loc) · 6.11 KB
/
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "Queue.h"
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int count = 0;
//void *malloc(size_t size);
/**
* For creating the queue.
*/
void create(Queue queue) {
queue->front = queue->rear = NULL;
queue->size = 0;
}
/**
* For creating the condition variable queue.
*/
void create_condQ(ConditionQueue q) {
q->front = NULL;
}
/* Returns queue size */
int getSize(Queue queue) {
return queue->size;
}
/**
* Enqueing in the order of increasing file size.
* @param data
*/
void enque_SJF(Queue que, RCB data) {
Node temp = que->front;
while (1) {
if (que->front == NULL) { //if the queue is empty.
Node newnode = (struct node *) malloc(1 * sizeof (struct node));
newnode->info = data;
newnode->ptr = NULL;
que->front = newnode;
que->rear = que->front;
break;
} else {
if (temp->ptr == NULL) { //if there is only one element in queue.
Node newnode = (struct node *) malloc(1 * sizeof (struct node));
newnode->info = data;
if (temp->info->sizeOfFile > data->sizeOfFile) { //if the size of file of current node is greater, then enqueue in front.
newnode->ptr = temp;
que->front = newnode;
break;
} else { //else enqueue at the rear.
newnode->ptr = NULL;
temp->ptr = newnode;
break;
}
} else {
if (temp->ptr->info->sizeOfFile > data->sizeOfFile) { //if size of file of current node is greater than data node, then insert the node.
Node newnode = (struct node *) malloc(1 * sizeof (struct node));
newnode->info = data;
newnode->ptr = temp->ptr;
temp->ptr = newnode;
if (temp == que->front)
que->front = newnode;
break;
} else
temp = temp->ptr;
}
}
}
que->size++;
}
/**
* This method enqueues the web request to the queue.
* @param que is the queue in which web request is to be enqueued.
* @param data is the web request to be enqueued.
*/
void enque(Queue que, RCB data) {
Node temp = que->front;
if (que->front == NULL) { //if the queue is empty.
Node newnode = (Node) malloc(1 * sizeof (struct node)); //assign the memory
newnode->ptr = NULL; //Assign NULL to next pointer.
newnode->info = data; //Assign the data to front node.
que->front = newnode;
que->rear = que->front; //Make front and rear equal.
que->size++;
return;
} else {
while (1) {
if (temp->ptr == NULL) { //if next node of queue in empty, insert the web request to the empty node.
Node newnode = (struct node *) malloc(1 * sizeof (struct node)); // Initialize a new node and assign memory to it.
newnode->info = data; // Assign web request data to it.
newnode->ptr = NULL;
temp->ptr = newnode; // Add the node to queue.
que->size++;
break; // Update found to true, so that while loop ends.
} else
temp = temp->ptr; //If the next node is not empty, move to the next node.
}
}
}
/**
* This method dequeues the element from the front of the queue.
* @param queue
* @return
*/
RCB deque(Queue queue) {
if (queue != NULL) { //Check if the queue is empty.
Node front1 = queue->front; //assign the from of the queue to a temporary variable.
RCB data = NULL;
if (front1 == NULL) {
return data;
} else {
if (front1->ptr != NULL) {
front1 = front1->ptr;
data = queue->front->info;
free(queue->front);
queue->front = front1;
} else {
data = queue->front->info;
free(queue->front);
queue->front = NULL;
queue->rear = NULL;
}
if (queue->size != 0)
queue->size--;
}
return data;
} else
return NULL;
}
/* Returns the front element of queue */
RCB frontelement(Queue queue) {
if ((queue->front != NULL) && (queue->rear != NULL))
return (queue->front->info);
else
return NULL;
}
/**
* This method enqueues the condition variables to the queue.
*/
void enque_Cond(pthread_cond_t* cond_worker, ConditionQueue q) {
CondNode temp = q->front;
if (temp == NULL) {
CondNode newnode = (struct cond_node*) malloc(1 * sizeof (struct cond_node));
newnode->data = cond_worker;
newnode->next = NULL;
q->front = newnode;
return;
} else {
while (1) {
if (temp->next == NULL) {
CondNode newnode = (struct cond_node*) malloc(1 * sizeof (struct cond_node));
newnode->data = cond_worker;
newnode->next = NULL;
temp->next = newnode;
break;
} else
temp = temp->next;
}
}
}
pthread_cond_t* DequeCond(ConditionQueue queue) {
if (queue != NULL) { //Check if the queue is empty.
CondNode front1 = queue->front; //assign the from of the queue to a temporary variable.
pthread_cond_t* data = NULL;
if (front1 == NULL) {
return data;
} else {
if (front1->next != NULL) {
front1 = front1->next;
data = queue->front->data;
free(queue->front);
queue->front = front1;
} else {
data = queue->front->data;
free(queue->front);
queue->front = NULL;
}
}
return data;
} else
return NULL;
}