Advertisement
leo11

Untitled

Jun 7th, 2021
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. typedef struct Map {
  2.     char* id;
  3.     char* value;
  4.     struct Map *next;
  5. } map;
  6.  
  7. map* head = NULL;
  8.  
  9. void add_elem(char* key, char* str){
  10.     if (head == NULL){
  11.         map* new = (map*) malloc (sizeof(map));
  12.         head = new;
  13.         new->id = (char*) malloc (strlen(key) +1);
  14.         strcpy(new->id, key);
  15.         new->value = (char*) malloc (strlen(str) + 1);
  16.         strcpy(new->value, str);
  17.         new->next = NULL;
  18.         return;
  19.     }
  20.  
  21.     map *tmp = head, *h = head;
  22.     head = head->next;
  23.  
  24.     while (head){
  25.         tmp = head;
  26.         head = head->next;
  27.     }
  28.  
  29.     map* new = (map*) malloc (sizeof(map));
  30.     tmp->next = new;
  31.     head = new;
  32.     new->id = (char*) malloc (strlen(key) + 1);
  33.     strcpy(new->id, key);
  34.     new->value = (char*) malloc (strlen(str) + 1);
  35.     strcpy(new->value, str);
  36.     new->next = NULL;
  37.  
  38.     head = h;
  39. }
  40.  
  41. void pop(char* id){
  42.     map* h = head;
  43.     if (head == NULL){
  44.         printf("Error!\n");
  45.         exit(1);
  46.     }
  47.  
  48.     map* tmp = head;
  49.     head = head->next;
  50.     if (head == NULL){
  51.         head = tmp;
  52.         free(head);
  53.         return;
  54.     }
  55.     if (strcmp(tmp->id, id) == 0){
  56.         free(tmp);
  57.         return;
  58.     }
  59.  
  60.     while (head && strcmp(head->id, id)){
  61.         tmp = head;
  62.         head = head->next;
  63.     }
  64.  
  65.     tmp->next = head->next;
  66.     free(head);
  67.  
  68.     head = h;
  69.     return;
  70. }
  71.  
  72. char* get_value(char* id){
  73.     map* h = head;
  74.     map* tmp = head;
  75.  
  76.     if (strcasecmp(tmp->id, id) == 0){
  77.         head = h;
  78.         return tmp->value;
  79.     }
  80.     head = head->next;
  81.     while (head){
  82.         if (strcasecmp(head->id, id) == 0){
  83.             head = h;
  84.             return tmp->next->value;
  85.         }
  86.         tmp = head;
  87.         head = head->next;
  88.     }
  89.     char* sss = "<unknown>";
  90.     head = h;
  91.     return sss;
  92.  
  93. }
  94.  
  95. void check_map(){
  96.     map* h = head;
  97.  
  98.     while (head){
  99.         printf("Key: %s, value: %s\n", head->id, head->value);
  100.         head = head->next;
  101.     }
  102.     head = h;
  103. }
  104.  
  105. void clean_buff(){
  106.     while(getchar() != '\n'){;}
  107. }
  108.  
  109.  
  110. int main(){
  111.  
  112.     char str[200];
  113.     char* ch = "ugabuga";
  114.     char** arr = (char**) malloc (200* sizeof(char*));
  115.     for (int i = 0; i < 200; i++)
  116.         arr[i] = (char*) malloc (200*sizeof(char));
  117.  
  118.     int d = 0;
  119.  
  120.     scanf("%s", str);
  121.     while(strcmp(str, ch)){
  122.         strcpy(arr[d], str);
  123.         d++;
  124.         scanf("%s", str);
  125.     }
  126.  
  127.     // for (int i = 0; i < d; i++)
  128.     //     printf("%s\n", arr[i]);
  129.  
  130.     for (int i = 0 ; i < d; i += 2){
  131.         add_elem(arr[i], arr[i+1]);
  132.     }
  133.  
  134.     clean_buff();
  135.  
  136.     char to_tr[200];
  137.     fgets(to_tr, 200, stdin);
  138.     to_tr[strlen(to_tr) - 1] = '\0';
  139.  
  140.     char* a[200];
  141.     for (int i = 0; i < 200; i++)
  142.         a[i] = (char*) malloc (200);
  143.     int q = 0;
  144.     char* pch = strtok(to_tr, " ");
  145.     while(pch != NULL){
  146.         strcpy(a[q], pch);
  147.         q++;
  148.         pch = strtok(NULL, " ");
  149.     }
  150.  
  151.     for (int i = 0; i < q-1; i++)
  152.         printf("%s ", get_value(a[i]));
  153.     printf("%s\n", get_value(a[q-1]));
  154.  
  155.    
  156.  
  157.  
  158.  
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement