-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_hetero.c
220 lines (210 loc) · 7.63 KB
/
queue_hetero.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "queue_hetero.h"
void push_by_type(char *type, void *data, struct hetero_queue **base, struct hetero_queue **top){
struct hetero_queue *temp = *base;
struct hetero_queue *type_temp = NULL;
int type_count = 0;
#ifdef __DEBUG__
if(strcmp(type, "string")==0)
printf("%s : The passed string is %s\n", __FUNCTION__, (char *)data);
#endif
if(temp == NULL){
temp = malloc(sizeof(struct hetero_queue));
temp->type = type;
temp->data = data;
temp->next = NULL;
*base = temp;
*top = *base;
}
else{
do{
if(strcmp(temp->type, type)==0){
type_temp = temp;
type_count++;
}
temp = temp->next;
}while(temp != NULL);
if(type_count==0){
/*
* in case we do not have any elements of the given type we will
* just push on the top
*/
type_temp = *top;
}
temp = malloc(sizeof(struct hetero_queue));
temp->type = type;
temp->data = data;
temp->next = type_temp->next;
type_temp->next=temp;
if(type_temp == *top)
*top = temp;
}
}
void push(char *type, void *data, struct hetero_queue **base, struct hetero_queue **top){
struct hetero_queue *temp = *top;
#ifdef __DEBUG__
if(strcmp(type, "string")==0)
printf("%s : The passed string is %s\n", __FUNCTION__, (char *)data);
#endif
if(temp == NULL){
#ifdef __DEBUG__
printf("%s : null\n",__FUNCTION__);
#endif
temp = malloc(sizeof(struct hetero_queue));
temp -> type = type ;
temp -> data = data;
temp->next = NULL;
*base = temp;
*top = *base;
}
else{
#ifdef __DEBUG__
printf("%s : not null\n",__FUNCTION__);
#endif
temp->next = malloc(sizeof(struct hetero_queue));
temp->next->type = type;
temp->next->data = data;
*top = temp->next;
}
}
struct hetero_queue * pop_by_type(char *type, struct hetero_queue **base){
struct hetero_queue *temp = *base;
struct hetero_queue *type_temp=NULL;
struct hetero_queue *prev_temp = temp;
struct hetero_queue *prev_type_temp = NULL;
if (temp == NULL){
printf("%s : You cannot pop an empty queue\n", __FUNCTION__);
return NULL;
}
do{
if(strcmp(temp->type, type)==0){
prev_type_temp = prev_temp;
type_temp = temp;
}
prev_temp = temp;
temp = temp->next;
}while(temp!=NULL);
if(type_temp == NULL){
printf("%s : The queue does not contain elements of type : %s\n",__FUNCTION__, type);
return NULL;
}
if(type_temp == prev_type_temp){
/*
* the case when the base element is the only element of specified type
*/
temp = *base;
*base = type_temp->next;
return temp;
}
/*
* now lets remove the queue element for all other cases
*/
prev_type_temp->next = type_temp->next;
return type_temp;
}
struct hetero_queue *pop(struct hetero_queue **base){
struct hetero_queue *temp = *base;
struct hetero_queue *prev_temp = temp;
while(temp->next != NULL){
temp=temp->next;
}
if(temp == prev_temp){
/*
* when there is only one element in the queue
*/
temp = *base;
*base = NULL;
return temp;
}
prev_temp->next = temp->next;
return temp;
}
int print_by_type(char *type, struct hetero_queue *base){
struct hetero_queue *temp = base;
int print_count=0, queue_count=0;
printf("________________________________________________\n");
if(temp == NULL){
printf("%s : The queue is empty\n", __FUNCTION__);
printf("________________________________________________\n");
return -1;
}
do{
queue_count++;
if(strcmp(temp->type, type)==0){
if(strcmp(type,"int")==0)
printf("<%3d> <%6s> %d \n",queue_count, temp->type, *(int *)(temp->data));
else if (strcmp(type, "string")==0)
printf("<%3d> <%6s> %s \n",queue_count, temp->type, (char *)temp->data);
else if (strcmp(type, "double")==0)
printf("<%3d> <%6s> %lf \n",queue_count, temp->type, *(double *)(temp->data));
else
printf("<%3d> <%6s> %s \n",queue_count, temp->type, (char *)(temp->data));
print_count++;
}
temp=temp->next;
}while(temp != NULL);
if (print_count == 0){
printf("%s : The queue does not conatin elements of type : %s\n",__FUNCTION__, type);
printf("________________________________________________\n");
return 0;
}
else{
printf("________________________________________________\n");
return print_count;
}
}
int print(struct hetero_queue *base){
struct hetero_queue *temp = base;
int queue_count=0;
printf("________________________________________________\n");
if(temp == NULL){
printf("%s : The queue is empty\n", __FUNCTION__);
printf("________________________________________________\n");
return -1;
}
do{
queue_count++;
if(strcmp(temp->type,"int")==0)
printf("<%3d> <%6s> %d \n",queue_count, temp->type, *(int *)temp->data);
else if (strcmp(temp->type, "string")==0)
printf("<%3d> <%6s> %s \n",queue_count, temp->type, (char *)temp->data);
else if (strcmp(temp->type, "double")==0)
printf("<%3d> <%6s> %lf \n",queue_count, temp->type, *(double *)temp->data);
temp=temp->next;
}while(temp != NULL);
printf("________________________________________________\n");
return queue_count;
}
int count_by_type(char *type, struct hetero_queue *base){
struct hetero_queue *temp = base;
int print_count=0, queue_count=0;
if(temp == NULL){
printf("%s : The queue is empty\n", __FUNCTION__);
return -1;
}
do{
queue_count++;
if(strcmp(temp->type, type)==0){
print_count++;
}
temp=temp->next;
}while(temp != NULL);
if (print_count == 0){
printf("%s : The queue does not conatin elements of type : %s\n",__FUNCTION__, type);
return 0;
}
else
return print_count;
}
int count(struct hetero_queue *base){
struct hetero_queue *temp = base;
int queue_count=0;
if(temp == NULL){
printf("%s : The queue is empty\n", __FUNCTION__);
return -1;
}
do{
queue_count++;
temp=temp->next;
}while(temp != NULL);
return queue_count;
}