Advertisement
Guest User

Untitled

a guest
May 5th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. void LIST_ENTRIES(entry *head){
  2.  
  3.     //make a pointer to entry called entry_ptr and initialize it to
  4.     //be the head node
  5.     entry *entry_ptr =  entry_head;
  6.    
  7.     if (entry_ptr == NULL) {
  8.         printf("no entries\n\n");
  9.     }
  10.    
  11.     //iterate through the list and print all the keys in it
  12.     while(entry_ptr != NULL){
  13.         printf("%s ", entry_ptr->key);
  14.    
  15.         //make a pointer to value and make it the head of the values
  16.         value *value_ptr = head->values;
  17.  
  18.         printf("[");
  19.  
  20.         //traverse the list to print all the values
  21.         while(value_ptr != NULL){
  22.             printf("%d ", value_ptr->value);
  23.             value_ptr = value_ptr->next;
  24.         }
  25.         printf("]\n\n");
  26.         entry_ptr = entry_ptr->next;
  27.     }
  28. }
  29.  
  30. //GET command
  31. void GET(char *key){
  32.  
  33.     //make a pointer to entry called entry_ptr and initialize it to
  34.     //be the head node
  35.     entry *entry_ptr = entry_head;
  36.     int has_key = 0;
  37.     int first_value = 1;
  38.    
  39.     if (entry_ptr == NULL) {
  40.         printf("no such key\n\n");
  41.         return;
  42.     }
  43.    
  44.     //traverse the list and when they given key matches the node,
  45.     //print all the values of that key
  46.     while(entry_ptr != NULL){
  47.         if(strcmp(entry_ptr->key, key) == 0 ){
  48.             has_key = 1;
  49.             value *value_ptr = entry_ptr->values;
  50.  
  51.             printf("[");
  52.             while(value_ptr != NULL){
  53.                 if (first_value) {
  54.                     printf("%d", value_ptr->value);
  55.                     value_ptr = value_ptr->next;
  56.                     first_value = 0;
  57.                 } else {
  58.                     printf(" %d", value_ptr->value);
  59.                     value_ptr = value_ptr->next;
  60.                 }
  61.             }
  62.             printf("]\n\n");
  63.             return;
  64.         }
  65.         entry_ptr = entry_ptr->next;
  66.     }
  67.     if (!has_key) {
  68.         printf("no such key\n\n");
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement