easy as 123

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Bacterie {
    char nume[30];
    int grad;
};

struct ListNode {
    struct Bacterie bacterie;
    struct ListNode * next;
};
 
struct LinkedList {
    struct ListNode * firstNode;
    unsigned size;
};

struct LinkedList * createLinkedList() {
    struct LinkedList * list = (struct LinkedList *) malloc(sizeof(struct LinkedList));
    list->firstNode == NULL;
    list->size = 0;
    return list;
}

unsigned linkedListSize(struct LinkedList * list) {
    return list->size;
}

struct ListNode *createNode(struct Bacterie bacterie) {
    struct ListNode * node = (struct ListNode *) malloc(sizeof(struct ListNode));
        node->bacterie = bacterie;
        node->next = NULL;
        return node;
}    

void linkedListInsert(struct LinkedList *list, struct Bacterie bacterie) {
    struct ListNode *tmpNode = createNode(bacterie);
    if (list->size == 0) {
        list->firstNode = tmpNode;
        list->size = 1;
        return;
    }
    if (tmpNode->bacterie.grad > list->firstNode->bacterie.grad) {
    	tmpNode->next = list->firstNode;
    	list->firstNode = tmpNode;
    	list->size++;
    	return;
	}   
    struct ListNode *tmpNode1 = list->firstNode;
    while (tmpNode1->next != NULL && tmpNode1->next->bacterie.grad >= tmpNode->bacterie.grad) {
    	tmpNode1 = tmpNode1->next;
    }
    if (tmpNode1->next == NULL) {
    	tmpNode1->next = tmpNode;
    	list->size++;
    	return;
    }
    tmpNode->next = tmpNode1->next;
    tmpNode1->next = tmpNode;
    list->size++;
}

char linkedListContains(struct LinkedList *list, struct Bacterie bacterie) {
	struct ListNode *tmpNode = list->firstNode;
	while (tmpNode != NULL) {
		if (strcmp(tmpNode->bacterie.nume, bacterie.nume) == 0) {
			return 1;
		}
		tmpNode = tmpNode->next;
	}
		return 0;
}
	    	   
void printList(struct LinkedList *list) {
	int i;
	struct ListNode *tmpNode = list->firstNode;
	for (i = 0; i < list->size; i++) {
		printf("%s", tmpNode->bacterie.nume);
		tmpNode = tmpNode->next;
	}  
}

void printFirst(struct LinkedList *list) {
	printf("%s\n", list->firstNode->bacterie.nume);
}
  
struct Bacterie readfromKeyboard() {
    struct Bacterie bacterie;
    printf("[Nume]:");
    fgets(bacterie.nume, 30, stdin);
    if (bacterie.nume[0] == '-') {
        return bacterie;
    }
    printf("[Grad de risc]:");
    scanf("%d", &bacterie.grad);
    getchar();
    return bacterie;
}

int main() {
	struct Bacterie bacterie[100];
	struct LinkedList *listaBacterii = createLinkedList();
	int i = 1;
	printf("[Bacteria 1]\n");
	bacterie[i] = readfromKeyboard();
	while (bacterie[i].nume[0] != '-') {
		if (linkedListContains(listaBacterii, bacterie[i]) == 0) {
			linkedListInsert(listaBacterii, bacterie[i]);
			i++;
		}
			printf("[Bacteria %d]\n", i);		
			bacterie[i] = readfromKeyboard();
	}
	printList(listaBacterii);
	printFirst(listaBacterii);
	return 0;
}
	
		
		
......................


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct Elev {
    char nume[30];
    char prenume[30];
    char cnp[14];
    unsigned media;
};

int compare(char *cnp1, char *cnp2) {
    return strcmp(cnp1, cnp2);
}

struct Pair {
    char * key;
    struct Elev elev;
};
 
struct TreeNode {
    struct TreeNode * parent;
    struct TreeNode * left;
    struct TreeNode * right;
    struct Pair pair;
};
 
struct TreeMap {
    struct TreeNode * root;
    unsigned size;
};

struct TreeMap * createTreeMap() {
    struct TreeMap *map = (struct TreeMap *) calloc(1, sizeof(struct TreeMap));
    return map;
}

unsigned treeMapSize(struct TreeMap * map) {
    return map->size;
}

char treeMapIsEmpty(struct TreeMap * map) {
    if (map->size == 0) {
        return 1;
    }
    return 0;
}

void treeMapPut(struct TreeMap * map, char * key, struct Elev elev) {
    if (map->size == 0) {
        struct TreeNode * newNode = (struct TreeNode *) malloc(sizeof(struct TreeNode));
        newNode->pair.key = key;
        newNode->pair.elev = elev;
        newNode->parent = newNode->left = newNode->right = NULL;
        map->root = newNode;
        map->size = 1;
        return;
    }
    struct TreeNode *tmpNode = map->root;
    while (1) {
    if (compare(tmpNode->pair.key, key) == 0) {
        tmpNode->pair.key = key;
        tmpNode->pair.elev = elev;
        return;
    }
    if (compare(tmpNode->pair.key, key) > 0 && tmpNode->left != NULL) {
        tmpNode = tmpNode->left;
    }
    if (compare(tmpNode->pair.key, key) > 0 && tmpNode->left == NULL) {
        struct TreeNode * newNode = (struct TreeNode *) malloc(sizeof(struct TreeNode));
        newNode->pair.key = key;
        newNode->pair.elev = elev;
        newNode->parent = newNode->left = newNode->right = NULL;
        newNode->parent = tmpNode;
        tmpNode->left = newNode;
        map->size++;
        return;
    }
    if (compare(tmpNode->pair.key, key) < 0 && tmpNode->right != NULL) {
        tmpNode = tmpNode->right;
    }
    if (compare(tmpNode->pair.key, key) < 0 && tmpNode->right == NULL) {
        struct TreeNode * newNode = (struct TreeNode *) malloc(sizeof(struct TreeNode));
        newNode->pair.key = key;
        newNode->pair.elev = elev;
        newNode->parent = newNode->left = newNode->right = NULL;
        newNode->parent = tmpNode;
        tmpNode->right = newNode;
        map->size++;
        return;
    }
    }
}

void printNode(struct TreeNode *node) {
    if (node == NULL) return;
    printNode(node->left);
    printf("%s", node->pair.elev.cnp);
    printNode(node->right);
}


void treePrint(struct TreeMap *map) {
    printNode(map->root);
}

void treeMapModify(struct TreeMap * map, char * key) {
    struct TreeNode *tmpNode = map->root;
    while (tmpNode != NULL) {
        if (compare(tmpNode->pair.key, key) == 0) {
            tmpNode->pair.elev.media = 0;
            return;
        }
        if (compare(tmpNode->pair.key, key) > 0) {
            tmpNode = tmpNode->left;
        }
        if (compare(tmpNode->pair.key, key) < 0) {
            tmpNode = tmpNode->right;
        }
    }
}

void printMap(struct TreeMap *map, char * key) {
    struct TreeNode *tmpNode = map->root;
    while (tmpNode != NULL) {
        if (compare(tmpNode->pair.key, key) == 0) {
            printf("[%s][%s] - RESPINS\n", tmpNode->pair.elev.nume, tmpNode->pair.elev.prenume);
            return;
        }
        if (compare(tmpNode->pair.key, key) > 0) {
            tmpNode = tmpNode->left;
        }
        if (compare(tmpNode->pair.key, key) < 0) {
            tmpNode = tmpNode->right;
        }
    }
}       

struct Elev readFromKeyboard() {
    struct Elev elev;
    printf("nume:");
    scanf("%s", elev.nume);
    printf("prenume:");
    scanf("%s", elev.prenume);
    printf("cnp:");
    scanf("%s", elev.cnp);
    printf("media:");
    scanf("%d", &elev.media);
    return elev;
}

int main() {
    struct Elev elev[100];
    struct TreeMap *map = createTreeMap();
    int i = 0;
    while(1) {
        elev[i] = readFromKeyboard();
        if (elev[i].media < 0 || elev[i].media > 10) {
            break;
        }
        treeMapPut(map,elev[i].cnp, elev[i]);
        i++;
    }
    treePrint(map);
    char cnp1[14], cnp2[14], cnp3[14];
        printf("primul cnp");
        scanf("%s", cnp1);
        treeMapModify(map, cnp1);
        printf("al doilea cnp");
        scanf("%s", cnp2);
        treeMapModify(map, cnp2);
        printf("al treilea cnp");
        scanf("%s", cnp3);
        treeMapModify(map, cnp3);
        printMap(map, cnp1);
        printMap(map, cnp2);
        printMap(map, cnp3);
        return 0;
}
Last edited on
I don't see a question.
Topic archived. No new replies allowed.