Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h> /* memset() */
  3. #include <stdlib.h> /* exit() */
  4. #include "linked.h"
  5.  
  6. /* Linked list example */
  7. int main(int argc, char **argv)
  8. {
  9.     struct linked_element_s *new_element = create_new_element("user1", "password");
  10.  
  11.     if (!new_element) {
  12.         return 1;
  13.     }
  14.  
  15.     if (add_to_list(new_element) != SUCCESS) {
  16.         fprintf(stderr, "Failed to add element to list\n");
  17.         exit(1);
  18.     } else {
  19.         printf("Added new element to list (now contains %d elements)", get_head()->count);
  20.     }
  21.  
  22.     new_element = create_new_element("user2", "password2");
  23.     if (add_to_list(new_element) != SUCCESS) {
  24.         fprintf(stderr, "Failed to add element to list\n");
  25.         exit(1);
  26.     } else {
  27.         printf("Added new element to list (now contains %d elements)\n", get_head()->count);
  28.     }
  29.  
  30.     dump_list();
  31.  
  32.     delete_user("user2");
  33.     dump_list();
  34.     return 0;
  35. }
  36.  
  37. /*
  38. * Render the list to stdout/
  39. */
  40. void dump_list(void)
  41. {
  42.     struct linked_element_s *roving = get_head()->next;
  43.     int count = 0;
  44.  
  45.     printf("List is currently ...\n");
  46.     for (; roving; roving = roving->next) {
  47.         printf("(%d) User: %s, Pass: %s\n", count, roving->name, roving->password);
  48.     }
  49.     return;
  50. }
  51.  
  52. /*
  53. * Create a new element to add to the list
  54. *
  55. * Parameters:
  56. *   name        The name field
  57. *   password    The password field
  58. *
  59. * Return values:
  60. *   (non-NULL)  The address of the new element
  61. *   NULL        An error occurred (out of memory)
  62. */
  63. struct linked_element_s *create_new_element(char *name, char *password)
  64. {
  65.     static struct linked_element_s new_element;
  66.  
  67.     memset(&new_element, 0, sizeof(new_element));
  68.     new_element.name = _strdup(name);
  69.     new_element.password = _strdup(password);
  70.  
  71.     if (!new_element.name || !new_element.password) {
  72.         fprintf(stderr, "Out of memory appending to the list\n");
  73.         return NULL;
  74.     }
  75.  
  76.     new_element.next = NULL;
  77.     return &new_element;
  78. }
  79.  
  80. /*
  81. * Get the list head
  82. */
  83. struct linked_head_s *get_head(void)
  84. {
  85.     static struct linked_head_s head = { NULL, 0 }; /* 'next' and 'count' initial values */
  86.     return &head;
  87. }
  88.  
  89. /*
  90. * Add an element to the list
  91. *
  92. * We will accept a pointer to a structure, allocate a new element
  93. * and add it to the lists
  94. *
  95. * Parameters:
  96. *   candidate   A pointer to a new element to add
  97. *
  98. * Return values:
  99. *   SUCCESS     The elemnent was added
  100. *   FAILURE     An error occurred
  101. *
  102. * Other notes:
  103. *   This function duplcates the provided structure and
  104. *   adds that copy to the list
  105. */
  106. int add_to_list(struct linked_element_s *candidate)
  107. {
  108.     if (!candidate) {
  109.         fprintf(stderr, "%s(): Error - invalid new element provided to add to list\n", __func__);
  110.         return FAILURE;
  111.     }
  112.  
  113.     /* Create a copy of the candidate new element */
  114.     struct linked_element_s *new_element = calloc(1, sizeof(struct linked_element_s));
  115.  
  116.     if (!new_element) {
  117.         fprintf(stderr, "Not enough RAM to add to the list\n");
  118.         return FAILURE;
  119.     } else {
  120.         memcpy(new_element, candidate, sizeof(struct linked_element_s));
  121.     }
  122.  
  123.     /* Point to first element in list (may be NULL) */
  124.     struct linked_element_s *roving = get_head()->next;
  125.  
  126.     /* Empty list - add first new element */
  127.     if (!roving) {
  128.         get_head()->next = new_element;
  129.         get_head()->count += 1;
  130.         return SUCCESS;
  131.     }
  132.  
  133.     /* List has at least 1 element: Locate end of the list */
  134.     for (; roving; roving = roving->next) {
  135.         if (!(roving->next)) {
  136.             /* Next element in list doesn't exist, so extend the list */
  137.             roving->next = new_element;
  138.             get_head()->count += 1;
  139.             printf("Added element to list\n");
  140.             return SUCCESS;
  141.         }
  142.     }
  143.  
  144.     return FAILURE; /* Assuming nothing went wrong we never reach here */
  145. }
  146.  
  147. /*
  148. * Delete an element from the list by username
  149. *
  150. * Parameters:
  151. *   username    The username to delete
  152. *
  153. * Return values:
  154. *   (none)
  155. */
  156. void delete_user(char *username)
  157. {
  158.     struct linked_element_s *roving = get_head()->next;
  159.  
  160.     if (!roving) {
  161.         /* List is empty */
  162.         return;
  163.     }
  164.  
  165.     /*
  166.     * Check for the special condition where the first element is for deletion
  167.     * We do this as we have a different structure for the head than the elements
  168.     * otherwise we could do it in a more generic fashion
  169.     */
  170.     if (!strcmp(roving->name, username)) {
  171.         /* We delete first element */
  172.         get_head()->next = roving->next; /* Remove from list */
  173.  
  174.         /* Delete dynamic contents then the element itself */
  175.         free(roving->name);
  176.         free(roving->password);
  177.         free(roving);
  178.         printf("Deleted: %s\n", username);
  179.         return;
  180.     }
  181.  
  182.     struct linked_element_s *tmp = roving; /* Record the element previous to the one we're checking in the loop below */
  183.  
  184.     for (roving = roving->next; roving; roving = roving->next) {
  185.         if (!strcmp(roving->name, username)) {
  186.             /* Delete from the second element onwards */
  187.             tmp->next = roving->next;
  188.             free(roving->name);
  189.             free(roving->password);
  190.             free(roving);
  191.             printf("Deleted: %s\n", username);
  192.             return;        
  193.         } else {
  194.             tmp = roving; /* Keep tmp one behind roving */
  195.         }
  196.     }
  197.     return;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement