rrz0

Segmentation Error Code

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