Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. // ====== this is in data.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "data.h"
  6.  
  7. // Input: ’in_name’: a string ends with ’\0’
  8. //        ’in_id’: an integer
  9. // Output: a pointer of type pointer to Key,
  10. //         pointing to an allocated memory containing a Key
  11. // Effect: dynamically allocate memory to hold a Key
  12. //         set Key’s id to be in_id
  13. //         dynamically allocate memory for the Key’s name
  14. //         so that name will contain what is in ’in_name’.
  15. // Note: do not use strdup(), use malloc(), strlen(), and strcpy()
  16. Key *key_construct(char *in_name, int in_id) {
  17.     /*
  18.         - first use malloc(sizeof(Key))
  19.             - This dynamically allocate memory the size of the Key
  20.         - next use malloc((strlen(in_name)+1)*sizeof(char))
  21.             - This dynamically allocates memory the size of the in_name string
  22.         - then use strcpy()
  23.  
  24.     */
  25.  
  26.     Key* keyPoint = (Key*)malloc((sizeof(Key)));
  27.  
  28.     if (keyPoint == NULL) {
  29.         printf("Error: Memory Allocation Failure\n");
  30.         exit(EXIT_FAILURE);
  31.     }
  32.     else {
  33.         keyPoint->id = in_id;
  34.         keyPoint->name = (char*)malloc((strlen(in_name) + 1) * sizeof(char));
  35.         strcpy(&keyPoint->name, in_name);
  36.     }
  37.  
  38.     return keyPoint;
  39.    
  40.  
  41.    
  42. }
  43.  
  44. // Input: ’key1’ and ’key2’ are two pointers to Key
  45. // Output: if return value < 0, then *key1 < *key2,
  46. //         if return value = 0, then *key1 = *key2,
  47. //         if return value > 0, then *key1 > *key2,
  48. // Note: use strcmp() to compare key1->name and key2->name
  49. //       if key1->name == key2->name, then compare key1->id with key2->id
  50. int key_comp(Key *key1, Key *key2) {
  51.     int value = strcmp(key1->name, key2->name);
  52.  
  53.     if (value < 0) {
  54.         return -1;
  55.     }
  56.     else if (value > 0) {
  57.         return 1;
  58.     }
  59.     else {
  60.         if (key1->id < key2->id) {
  61.             return -1;
  62.         }
  63.         else if (key1->id == key2->id) {
  64.             return 0;
  65.         }
  66.         else {
  67.             return 1;
  68.         }
  69.     }
  70. }
  71.  
  72. // Input: ’key’: a pointer to Key
  73. // Effect: ( key->name key->id ) is printed
  74. void print_key(Key *key) {
  75.     printf("(%s %i)", key->name, key->id);
  76. }
  77.  
  78. // Input: ’node’: a node
  79. // Effect: node.key is printed and then the node.data is printed
  80. void print_node(Node node) {
  81.     print_key(node.key);
  82.     printf(" %i", node.data);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement