rrz0

passing strings to hashmap

Dec 19th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. //#pragma warning(disable:4996)
  2. // C program to implement basic operations (search, insert and delete)
  3. // on a BST that handles duplicates by storing count with every node
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include <string.h>
  7.  
  8. #define MAX_STRING_SIZE 100
  9. #define HASHSIZE 101  /* comment here */
  10. #define FILE_OUTPUT "calendar.txt"
  11. #define ALLOCSIZE 1000 /*size of available space*/
  12.  
  13. static char allocbuf[ALLOCSIZE]; /* storage for alloc*/
  14. static char *allocp = allocbuf; /* next free position*/
  15. static struct map *hashtab[HASHSIZE]; /* pointer table */
  16. unsigned hash(char *); /* hashing function: form hash value for string s. used by both lookup and install*/
  17. char *strdup(char *);
  18.  
  19. char *alloc(int n) { /* return a pointer to n characters*/
  20.     if (allocbuf + ALLOCSIZE - allocp >= n) { /*it fits*/
  21.         allocp += n;
  22.         return allocp - n; /*old p*/
  23.     } else /*not enough room*/
  24.         return 0;
  25. }
  26.  
  27. typedef struct map { /* creating a map structure */
  28.     struct map *next;
  29.     char *value;
  30.     char *KEY;
  31. } map;
  32.  
  33. /* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
  34. map *lookup(char *s) {
  35.     map *np;
  36.  
  37.     for (np = hashtab[hash(s)]; np != NULL; np = np->next) {
  38.         if (strcmp(s, np->KEY) == 0) {
  39.             return np;
  40.         }
  41.     }
  42.     return NULL;
  43. }
  44.  
  45. /* insert function takes a pointer to a char - KEY, value and returns a pointer to map structure */
  46. map *insert_map(char* KEY, char* value) {
  47.     /* install: put (name, defn) in */
  48.     /* Install uses lookup to determine whether the KEY being installed
  49.        is already present. Proceeds to create a new entry or update*/
  50.     map *np;
  51.     unsigned hashval;
  52.  
  53.     if ((np = lookup(KEY)) == NULL) {
  54.         np = (map *) malloc(sizeof (*np));
  55.         if (np == NULL || (np->KEY = strdup(KEY)) == NULL) {
  56.             return NULL;
  57.         }
  58.         hashval = hash(KEY);
  59.         np->next = hashtab[hashval];
  60.         hashtab[hashval] = np;
  61.     } else {
  62.         free((void *) np->value);
  63.     }
  64.     if ((np->value = strdup(value)) == NULL) { //map has no field value
  65.         return NULL;
  66.     }
  67.  
  68.     return np;
  69. }
  70.  
  71. /* simple implementation of strdup*/
  72. char *strdup(char *s) {
  73.  
  74.     char *d = malloc(strlen(s) + 1); // Space for length plus null
  75.     if (d == NULL) return NULL; // No memory
  76.     strcpy(d, s); // Copy the characters
  77.     return d; // Return the new string
  78. }
  79.  
  80. unsigned hash(char *s) {
  81.     /* hashing function: form hash value for string s */
  82.     /* used by both lookup and install*/
  83.     unsigned hashval;
  84.  
  85.     for (hashval = 0; *s != '\0'; s++) {
  86.         hashval = *s + 31 * hashval;
  87.     }
  88.     return hashval % HASHSIZE;
  89. }
  90.  
  91. int main() {
  92.     char *value[10];
  93.     int unique_key_arr[10] = {5122018, 4122018, 3122018, 2122018}; // Make dynamic
  94.     char *value_arr[4] = {"Value1", "Value2", "Value3", "Value4"}; // an array of pointers
  95.     char KEY[7];
  96.     char *KEY_arr[4];
  97.  
  98.     for (int i = 0; i < 4; i++) {
  99.         char* p; //pointer for value
  100.         char* p2; //pointer for key
  101.  
  102.         if ((p = alloc(strlen(value_arr[i]) + 1)) == NULL) {
  103.             return -1;
  104.         } else {
  105.             strcpy(p, value_arr[i]); //copying value to pointer to char
  106.             value[i] = p; //p passed to value array
  107.             printf("%s\n", value[i]);
  108.         }
  109.  
  110.         //Convert integer to pointer to char
  111.         sprintf(KEY, "%d", unique_key_arr[i]);
  112.         KEY_arr[i] = KEY;
  113.         printf("%s\n", KEY_arr[i]);
  114.         //printf("I am here2 %s\n", KEY_arr[i]);
  115.     }
  116.  
  117.     map * table[10]; //pointer to type map structure
  118.  
  119.     printf("\nBefore insert\n");
  120.     for (int i = 0; i < 4; i++) {
  121.         printf("%s, %s\n", KEY_arr[i], value[i]);
  122.         table[i] = insert_map(KEY_arr[i], value[i]);
  123.     }
  124.  
  125.     printf("\n");
  126.  
  127.     for (int i = 0; i < 4; i++) {
  128.         printf("%s->%s \n", table[i]->KEY, table[i]->value);
  129.     }
  130.     printf("\n");
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment