-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
201 lines (171 loc) · 3.6 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
201
#include "queue.h"
#include <stdlib.h>
#include <stdio.h>
/*
typedef struct{
int id;
int lamport_clock;
int meeting_id;
int cylon_id;
queue_entry* next;
} queue_entry;
typedef struct{
int id;
queue_entry* entries;
} r_queue;
*/
//RULES
/*
* 1. If entry's meeting_id is set than it's nomovable.
* 2. First possible position for new entry is below last entry with set meeting_id and cylon_id.
*/
//QUEUE FUNCTIONS
r_queue* queue_init(int id)
{
r_queue* queue;
queue = malloc(sizeof(r_queue));
if(queue==NULL)
return queue;
queue->id = id;
queue->entries=malloc(sizeof(queue_entry));
queue->entries->id=-1;
queue->entries->lamport_clock=-1;
queue->entries->next=NULL;
return queue;
}
int queue_free(r_queue* queue)
{
queue_entry* field = queue->entries;
while(field!=NULL)
{
queue_entry* rest = field->next;
free(field);
field=rest;
}
free(queue);
return 0;
}
int queue_add(r_queue* queue, int send_id, int lamport_clock)
{
queue_entry* entry = malloc(sizeof(queue_entry));
int i=1;
if(entry==NULL)
return -1;
entry->id=send_id;
entry->lamport_clock=lamport_clock;
entry->meeting_id=-1;
entry->cylon_id=-1;
entry->next=NULL;
queue_entry* field = queue->entries;
while((field->next!=NULL) &&
((field->next->lamport_clock<lamport_clock) || ((field->next->lamport_clock==lamport_clock) && (field->next->id<send_id))))
{
field=field->next;
i++;
}
entry->next = field->next;
field->next = entry;
return i;
}
int queue_set_meeting(r_queue* queue, int id, int meeting)
{
queue_entry* field = queue->entries->next;
while(field!=NULL && field->id!=id)
field=field->next;
if(field==NULL)
return -1;
field->meeting_id=meeting;
return 0;
}
int queue_set_cylon(r_queue* queue, int id, int cylon)
{
queue_entry* field = queue->entries->next;
while(field!=NULL && field->id!=id)
field=field->next;
if(field==NULL)
return -1;
field->cylon_id=cylon;
return 0;
}
void queue_sort(r_queue* queue)
{
}
queue_entry* queue_get(r_queue* queue, int id)
{
queue_entry* field = queue->entries->next;
while(field!=NULL && field->id!=id)
field=field->next;
return field;
}
int queue_remove(r_queue* queue, int id)
{
queue_entry* field = queue->entries;
while(field->next!=NULL && field->next->id!=id)
field=field->next;
if(field->next==NULL)
return -1;
queue_entry* entry = field->next;
field->next = entry->next;
free(entry);
return 0;
}
int queue_to_array(r_queue* queue, queue_entry array[])
{
int q_size = queue_size(queue);
int a_size = sizeof(array)/sizeof(array[0]);
int i = 0;
if(q_size<a_size)
return -1;
queue_entry* entry = queue->entries->next;
while(entry!=NULL)
{
array[i]=*entry;
array[i++].next=NULL;
entry=entry->next;
}
return 0;
}
int queue_size(r_queue* queue)
{
int i=-1;
queue_entry* entry = queue->entries;
while(entry!=NULL)
{
i++;
entry=entry->next;
}
return i;
}
int queue_top(r_queue* queue, int *id, int *lamport)
{
queue_entry* entry = queue->entries->next;
if(entry==NULL)
return -1;
*id=entry->id;
*lamport=entry->lamport_clock;
return 0;
}
int queue_position(r_queue* queue, int id)
{
int i=0;
queue_entry* field = queue->entries->next;
while(field!=NULL && field->id!=id)
{
field=field->next;
i++;
}
if(field==NULL)
return -1;
return i;
}
int queue_get_id(r_queue* queue, int num)
{
int i=-1;
queue_entry* entry = queue->entries;
while(entry!=NULL && i!=num)
{
i++;
entry=entry->next;
}
return entry!=NULL ? entry->id : -1;
}