Advertisement
leo11

dict C

Jun 6th, 2021
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5.  
  6. typedef struct Map {
  7.     uint id;
  8.     char* value;
  9.     struct Map *next;
  10. } map;
  11.  
  12. map* head = NULL;
  13.  
  14. void add_elem(uint key, char* str){
  15.     if (head == NULL){
  16.         map* new = (map*) malloc (sizeof(map));
  17.         head = new;
  18.         new->id = key;
  19.         new->value = (char*) malloc (strlen(str) + 1);
  20.         strcpy(new->value, str);
  21.         new->next = NULL;
  22.         return;
  23.     }
  24.  
  25.     map *tmp = head, *h = head;
  26.     head = head->next;
  27.  
  28.     while (head){
  29.         tmp = head;
  30.         head = head->next;
  31.     }
  32.  
  33.     map* new = (map*) malloc (sizeof(map));
  34.     tmp->next = new;
  35.     head = new;
  36.     new->id = key;
  37.     new->value = (char*) malloc (strlen(str) + 1);
  38.     strcpy(new->value, str);
  39.     new->next = NULL;
  40.  
  41.     head = h;
  42. }
  43.  
  44. void pop(uint id){
  45.     map* h = head;
  46.     if (head == NULL){
  47.         printf("Error!\n");
  48.         exit(1);
  49.     }
  50.  
  51.     map* tmp = head;
  52.     head = head->next;
  53.     if (head == NULL){
  54.         head = tmp;
  55.         free(head);
  56.         return;
  57.     }
  58.     if (tmp->id == id){
  59.         free(tmp);
  60.         return;
  61.     }
  62.  
  63.     while (head && head->id != id){
  64.         tmp = head;
  65.         head = head->next;
  66.     }
  67.  
  68.     tmp->next = head->next;
  69.     free(head);
  70.  
  71.     head = h;
  72.     return;
  73. }
  74.  
  75. char* get_value(uint id){
  76.     map* h = head;
  77.     map* tmp = head;
  78.  
  79.     if (tmp->id == id){
  80.         head = h;
  81.         return tmp->value;
  82.     }
  83.     head = head->next;
  84.     while (head){
  85.         if (head->id == id){
  86.             head = h;
  87.             return tmp->next->value;
  88.         }
  89.         tmp = head;
  90.         head = head->next;
  91.     }
  92.     printf("id incorrect!\n");
  93.     head = h;
  94.     return NULL;
  95.  
  96. }
  97.  
  98. void check_map(){
  99.     map* h = head;
  100.  
  101.     while (head){
  102.         printf("Key: %d, value: %s\n", head->id, head->value);
  103.         head = head->next;
  104.     }
  105.     head = h;
  106. }
  107.  
  108. int main(){
  109.  
  110.     add_elem(23, "Hello world for this dict");
  111.     add_elem(12, "ultron is so-so");
  112.     add_elem(34, "Avengers are good");
  113.     add_elem(33, "vision isnt create yet");
  114.  
  115.     check_map();
  116.  
  117.     pop(12);
  118.     printf("\n");
  119.     check_map();
  120.  
  121.     pop(23);
  122.     printf("\n");
  123.     check_map();
  124.     printf("\n");
  125.  
  126.  
  127.     add_elem(123, "Work map?");
  128.     add_elem(234, "seems to work!");
  129.     check_map();
  130.     printf("\n");
  131.  
  132.     printf("Value:::: %s\n", get_value(33));
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement