rrz0

Untitled

Dec 19th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.89 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. typedef struct node {
  34.     int KEY;
  35.     char *value;
  36.     int node_count;
  37.     struct node *left, *right;
  38.     int unique_count;
  39. } node;
  40.  
  41.  
  42. // A utility function to create a new BST node
  43.  
  44. node *newNode(int KEY, char *value) {
  45.     //Allocate a new overall structure
  46.     node *temp = (node *) malloc(sizeof (node));
  47.  
  48.     //Copy the integer key
  49.     temp->KEY = KEY;
  50.     temp->value = value;
  51.  
  52.     temp->left = temp->right = NULL;
  53.     temp->node_count = 1;
  54.     return temp;
  55. }
  56.  
  57. // A utility function to do inorder traversal of BST
  58.  
  59. void inorder(node *root, int *pin_count) {
  60.     if (root != NULL) {
  61.         inorder(root->left, pin_count);
  62.         printf("%d(%d)\n", root->KEY, root->node_count);
  63.         inorder(root->right, pin_count);
  64.     }
  65. }
  66.  
  67. /* A utility function to find deepest keys of BST */
  68.  
  69. /* This is done by extracting the key with the lowest count,
  70. since count member of node struct gets incremented each time it is read. */
  71. void unique_key(node *root, int *p_unique_count, int p_unique_key_arr[], char *p_value_arr[]) {
  72.     int count = 0;
  73.     char *p;
  74.  
  75.     if (root != NULL) {
  76.         unique_key(root->left, p_unique_count, p_unique_key_arr, p_value_arr);
  77.         if (root->node_count == 1) {
  78.  
  79.             if ((p = alloc(strlen(root[count].value) + 1)) == NULL) {
  80.                 return;
  81.             } else {
  82.                 root[count].unique_count = *p_unique_count;
  83.                 p_unique_key_arr[*p_unique_count] = root[count].KEY;
  84.  
  85.                 strcpy(p, root[count].value);
  86.                 p_value_arr[*p_unique_count] = p;
  87.                 //printf("%s\n",p_value_arr[*p_unique_count][*p_unique_count]);
  88.  
  89.                 printf("%d(%d) -> %s %d\n", root->KEY, root->node_count, root->value, root->unique_count);
  90.                 (*p_unique_count)++;
  91.                 count++;
  92.             }
  93.         }
  94.         unique_key(root->right, p_unique_count, p_unique_key_arr, p_value_arr);
  95.     }
  96. }
  97.  
  98. /* A utility function to insert a new node with given key in BST */
  99. node* insert_node(node* node, int key, char *value) {
  100.     /* If the tree is empty, return a new node */
  101.     if (node == NULL)
  102.         return newNode(key, value);
  103.  
  104.     // If key already exists in BST, icnrement count and return
  105.     if (key == node->KEY) {
  106.         (node->node_count)++;
  107.         //  return node;
  108.     }
  109.  
  110.     /* Otherwise, recur down the tree */
  111.     if (key < node->KEY)
  112.         node->left = insert_node(node->left, key, value);
  113.     else
  114.         node->right = insert_node(node->right, key, value);
  115.  
  116.     /* return the (unchanged) node pointer */
  117.     return node;
  118. }
  119.  
  120. /* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
  121. map *lookup(char *s) {
  122.     map *np;
  123.  
  124.     for (np = hashtab[hash(s)]; np != NULL; np = np->next) {
  125.         if (strcmp(s, np->KEY) == 0) {
  126.             return np;
  127.         }
  128.     }
  129.     return NULL;
  130. }
  131.  
  132. /* insert function takes a pointer to a char - KEY, value and returns a pointer to map structure */
  133. map *insert_map(char* KEY, char* value) {
  134.     /* install: put (name, defn) in */
  135.     /* Install uses lookup to determine whether the KEY being installed
  136.        is already present. Proceeds to create a new entry or update*/
  137.     map *np;
  138.     unsigned hashval;
  139.  
  140.     if ((np = lookup(KEY)) == NULL) {
  141.         np = (map *) malloc(sizeof (*np));
  142.         if (np == NULL || (np->KEY = strdup(KEY)) == NULL) {
  143.             return NULL;
  144.         }
  145.         hashval = hash(KEY);
  146.         np->next = hashtab[hashval];
  147.         hashtab[hashval] = np;
  148.     } else {
  149.         free((void *) np->value);
  150.     }
  151.     if ((np->value = strdup(value)) == NULL) { //map has no field value
  152.         return NULL;
  153.     }
  154.  
  155.     return np;
  156. }
  157.  
  158. /* uninstall function removes a key and calendar from table maintained by lookup and install */
  159. map *remove_map(char *KEY) {
  160.     map *found;
  161.  
  162.     found = lookup(KEY);
  163.  
  164.     if (found == NULL) /* not found and nothing to do */ {
  165.         printf("Nothing to delete.");
  166.         return NULL;
  167.     } else {
  168.         if (found->next != NULL) {
  169.             found->next = found->next->next;
  170.             found = found->next;
  171.         } else {
  172.             hashtab[hash(KEY)] = NULL;
  173.             free((void *) found);
  174.         }
  175.     }
  176.     return found;
  177. }
  178.  
  179. /* simple implementation of strdup*/
  180. char *strdup(char *s) {
  181.  
  182.     char *d = malloc(strlen(s) + 1); // Space for length plus null
  183.     if (d == NULL) return NULL; // No memory
  184.     strcpy(d, s); // Copy the characters
  185.     return d; // Return the new string
  186. }
  187.  
  188. unsigned hash(char *s) {
  189.     /* hashing function: form hash value for string s */
  190.     /* used by both lookup and install*/
  191.     unsigned hashval;
  192.  
  193.     for (hashval = 0; *s != '\0'; s++) {
  194.         hashval = *s + 31 * hashval;
  195.     }
  196.     return hashval % HASHSIZE;
  197. }
  198.  
  199. // Driver Program to test above functions
  200.  
  201. int main() {
  202.     FILE *output_stream;
  203.     output_stream = fopen("calendar.txt", "w+");
  204.     int unique_count = 0;
  205.     int in_count, count_create = 0;
  206.     int count_delete =0;
  207.     char *value[100];
  208.     int unique_key_arr[10]; // Make dynamic
  209.     char *value_arr[10]; // an array of pointers [unique][chars]
  210.     char KEY[7];
  211.     char *KEY_arr_create[10];
  212.     char *KEY_arr_delete[10];
  213.  
  214.     //unique *data = NULL;
  215.  
  216.     /* READ CALENDER_OPERATIONS*/
  217.     /* Pass all operations down to BST*/
  218.  
  219.     /* Let us create following BST.  Passing values along with key */
  220.     node *root = NULL;
  221.     //node *root = (node*)malloc(sizeof(node));
  222.     node *test = (node*) malloc(sizeof (node));
  223.  
  224.     //this is for storing commands
  225.     root = insert_node(root, 2082018, "C 1200 79 Meeting");
  226.     root = insert_node(root, 3082018, "C 1300 60 Lunch");
  227.     root = insert_node(root, 4082018, "C 1400 30 Dinner");
  228.     root = insert_node(root, 5082018, "C 0600 90 Work");
  229.     root = insert_node(root, 6082018, "D 4300 30 Diy");
  230.     root = insert_node(root, 7082018, "C 5608 30 Dinner");
  231.     root = insert_node(root, 8082018, "C 1409 35 to");
  232.     root = insert_node(root, 9082018, "C 1600 60 play");
  233.     root = insert_node(root, 9082018, "U 1800 88 e");
  234.  
  235.     printf("Inorder traversal of the given tree \n");
  236.     inorder(root, &in_count);
  237.  
  238.     printf("\nDeepest unique keys of the given tree \n");
  239.     unique_key(root, &unique_count, unique_key_arr, value_arr);
  240.     printf("\n");
  241.  
  242.     printf("\nDeepest unique keys of the given tree in main\n");
  243.     for (int i = 0; i < unique_count; i++) {
  244.         printf("%d [%s]\n", unique_key_arr[i], value_arr[i]);
  245.         if (*value_arr[i] == 'C') { //Compare first char of pointer type
  246.             printf("I am Create\n");
  247.         } else if (*value_arr[i] == 'U') { //Compare first char of pointer type
  248.             printf("I am Update\n");
  249.         } else if (*value_arr[i] == 'D') { //Compare first char of pointer type
  250.             printf("I am Delete\n");
  251.         }
  252.     }
  253.  
  254.  
  255.     printf("\n");
  256.  
  257.     /* Map is for storing calendar. This should contain the unique elements
  258.      * from the BST above. Based on the first char inside `value_arr`, we
  259.      * should insert or delete a map. Number of operations to perform
  260.       depends on variable `unique_count`. Use strcpy()*/
  261.  
  262.  
  263.     printf("\nDeepest unique keys to map structure\n");
  264.     for (int i = 0; i < unique_count; i++) {
  265.         char* p; //pointer for value
  266.         char* p2; //pointer for key
  267.  
  268.         if (*value_arr[i] == 'C' || *value_arr[i] == 'U') { //Compare first char of pointer type
  269.  
  270.             if ((p = alloc(strlen(value_arr[count_create]) + 1)) == NULL) {
  271.                 return -1;
  272.             } else {
  273.                 strcpy(p, value_arr[count_create]); //copying value to pointer to char
  274.                 value[count_create] = p; //p passed to value array
  275.             }
  276.             if ((p2 = alloc(strlen(KEY) + 1)) == NULL) {
  277.                 return -1;
  278.             } else {
  279.                 //Convert integer to pointer to char
  280.                 sprintf(p2, "%d", unique_key_arr[count_create]);
  281.                 KEY_arr_create[count_create] = p2; //integer with unique key passed to array
  282.             }
  283.            
  284.             printf("I am here %s, %s\n", KEY_arr_create[count_create], value[count_create]);
  285.             count_create++;
  286.         }
  287.         else {
  288.             if ((p2 = alloc(strlen(KEY) + 1)) == NULL) {
  289.                 return -1;
  290.             } else {
  291.                 //Convert integer to pointer to char
  292.                 sprintf(p2, "%d", unique_key_arr[count_delete]);
  293.                 KEY_arr_delete[count_delete] = p2; //integer with unique key passed to array
  294.                 count_delete++;
  295.             }
  296.         }
  297.     }
  298.  
  299.     map * table[10]; //pointer to type map structure
  300.  
  301.     printf("\nBefore insert CREATE %d\n", count_create);
  302.     for (int i = 0; i < count_create; i++) {
  303.         printf("CREATE %s, %s\n",KEY_arr_create[i], value[i]);
  304.         //table[i] = insert_map(KEY_arr_create[i], value[i]);
  305.     }
  306.    
  307.     printf("\nBefore insert DELETE %d\n", count_delete);
  308.     for (int i = 0; i < count_delete; i++) {
  309.         printf("DELETE %s, %s\n",KEY_arr_delete[i], value[i]);
  310.         //table[i] = insert_map(KEY_arr_delete[i], value[i]);
  311.     }
  312.  
  313.     printf("\nContents of calendar (hard coded atm) \n");
  314.     for (int i = 0; i < unique_count; i++) {
  315.         //printf("%s->%s \n", table[i]->KEY, table[i]->value);
  316.     }
  317.     printf("\n");
  318.  
  319.     remove_map("4082018");
  320.     //uninstall("key3");
  321.  
  322.     map *result;
  323.  
  324.     char *keys[8] = {
  325.         "3082018",
  326.         "4082018",
  327.         "5082018",
  328.         "6082018",
  329.         "7082018",
  330.         "8082018",
  331.         "9082018",
  332.         "10082018"
  333.     };
  334.  
  335.     for (int i = 0; i < 8; i++) {
  336.         if ((result = lookup(keys[i])) == NULL) {
  337.             printf("key not found\n");
  338.         } else {
  339.             printf("%s->%s \n", result->KEY, result->value);
  340.             fprintf(output_stream, "%s->%s \n", result->KEY, result->value);
  341.         }
  342.     }
  343.     printf("\n");
  344.  
  345.     return 0;
  346. }
Add Comment
Please, Sign In to add comment