Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int m;
- #define hash_func(x) ((x) % m)
- char t_at[3] = "AT";
- char t_as[7] = "ASSIGN";
- int i, n, ind = 1, j = 0;
- struct hash {
- struct hash *next;
- int key;
- int value;
- } node;
- struct hash **array;
- void inithash (struct hash *s)
- {
- i = 0;
- array = (struct hash**)malloc(m * sizeof (struct hash*));
- while (i < m) {
- array[i] = NULL;
- i++;
- }
- }
- void find (int val)
- {
- struct hash *turn;
- turn = array [hash_func(val)];
- //skip (turn, val);
- //std_chk(val);
- while (turn != NULL && turn->key != val) turn = turn->next;
- if (NULL != turn) printf("%d\n", turn->value);
- else printf("0\n");
- }
- void delete (int k, int v)
- {
- struct hash *dummy, *dummy2;
- dummy = NULL;
- dummy2 = array[hash_func(k)];
- if (NULL != dummy2 && k != dummy2) {
- dummy = dummy2;
- dummy2 = dummy2->next;
- return;
- }
- if (NULL != dummy2 && k == dummy2) {
- if (NULL == dummy) {
- dummy = array[hash_func(k)];
- array[hash_func(k)] = dummy->next;
- free(dummy);
- return;
- }
- dummy2 = dummy->next;
- dummy->next = dummy2->next;
- free(dummy2);
- }
- }
- void insert (int k, int v)
- {
- struct hash *turn, *dummy;
- turn = array[hash_func(k)];
- void indumm (int key, int value) {
- dummy = malloc(ind * sizeof(struct hash));
- dummy->key = k;
- dummy->value = v;
- dummy->next = array[hash_func(k)];
- array[hash_func(k)] = dummy;
- }
- //skip (turn, v);
- //std_chk(v);
- while (turn != NULL && turn->key != k) turn = turn->next;
- if (0 == v) delete (k, v);
- if (0 != v && NULL != turn) turn->value = v;
- else indumm (k, v);
- }
- void mem_salvation (void)
- {
- struct hash *dummy;
- for (i = 0; i < m; i++) {
- if (NULL != array[i]) {
- while (array[i] != NULL) {
- dummy = array[i];
- array[i] = array[i]->next;
- free(dummy);
- }
- }
- }
- free(array);
- //printf(">memfreed\n");
- }
- int main()
- {
- struct hash turn;
- int value_a, value_b, value_c, i;
- char cmm[7] = { 0 };
- scanf("%d", &n);
- scanf("%d", &m);
- inithash(&turn);
- for (i = 0; i < n; i++) {
- scanf("%s", cmm);
- if (0 == strcmp(cmm, t_at)) {
- scanf ("%d", &value_a);
- find (value_a);
- continue;
- }
- if (0 == strcmp(cmm, t_as)) {
- scanf ("%d %d", &value_b, &value_c);
- insert (value_b, value_c);
- }
- }
- mem_salvation();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement