Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h> /* memset() */
- #include <stdlib.h> /* exit() */
- #include "linked.h"
- /* Linked list example */
- int main(int argc, char **argv)
- {
- struct linked_element_s *new_element = create_new_element("user1", "password");
- if (!new_element) {
- return 1;
- }
- if (add_to_list(new_element) != SUCCESS) {
- fprintf(stderr, "Failed to add element to list\n");
- exit(1);
- } else {
- printf("Added new element to list (now contains %d elements)", get_head()->count);
- }
- new_element = create_new_element("user2", "password2");
- if (add_to_list(new_element) != SUCCESS) {
- fprintf(stderr, "Failed to add element to list\n");
- exit(1);
- } else {
- printf("Added new element to list (now contains %d elements)\n", get_head()->count);
- }
- dump_list();
- delete_user("user2");
- dump_list();
- return 0;
- }
- /*
- * Render the list to stdout/
- */
- void dump_list(void)
- {
- struct linked_element_s *roving = get_head()->next;
- int count = 0;
- printf("List is currently ...\n");
- for (; roving; roving = roving->next) {
- printf("(%d) User: %s, Pass: %s\n", count, roving->name, roving->password);
- }
- return;
- }
- /*
- * Create a new element to add to the list
- *
- * Parameters:
- * name The name field
- * password The password field
- *
- * Return values:
- * (non-NULL) The address of the new element
- * NULL An error occurred (out of memory)
- */
- struct linked_element_s *create_new_element(char *name, char *password)
- {
- static struct linked_element_s new_element;
- memset(&new_element, 0, sizeof(new_element));
- new_element.name = _strdup(name);
- new_element.password = _strdup(password);
- if (!new_element.name || !new_element.password) {
- fprintf(stderr, "Out of memory appending to the list\n");
- return NULL;
- }
- new_element.next = NULL;
- return &new_element;
- }
- /*
- * Get the list head
- */
- struct linked_head_s *get_head(void)
- {
- static struct linked_head_s head = { NULL, 0 }; /* 'next' and 'count' initial values */
- return &head;
- }
- /*
- * Add an element to the list
- *
- * We will accept a pointer to a structure, allocate a new element
- * and add it to the lists
- *
- * Parameters:
- * candidate A pointer to a new element to add
- *
- * Return values:
- * SUCCESS The elemnent was added
- * FAILURE An error occurred
- *
- * Other notes:
- * This function duplcates the provided structure and
- * adds that copy to the list
- */
- int add_to_list(struct linked_element_s *candidate)
- {
- if (!candidate) {
- fprintf(stderr, "%s(): Error - invalid new element provided to add to list\n", __func__);
- return FAILURE;
- }
- /* Create a copy of the candidate new element */
- struct linked_element_s *new_element = calloc(1, sizeof(struct linked_element_s));
- if (!new_element) {
- fprintf(stderr, "Not enough RAM to add to the list\n");
- return FAILURE;
- } else {
- memcpy(new_element, candidate, sizeof(struct linked_element_s));
- }
- /* Point to first element in list (may be NULL) */
- struct linked_element_s *roving = get_head()->next;
- /* Empty list - add first new element */
- if (!roving) {
- get_head()->next = new_element;
- get_head()->count += 1;
- return SUCCESS;
- }
- /* List has at least 1 element: Locate end of the list */
- for (; roving; roving = roving->next) {
- if (!(roving->next)) {
- /* Next element in list doesn't exist, so extend the list */
- roving->next = new_element;
- get_head()->count += 1;
- printf("Added element to list\n");
- return SUCCESS;
- }
- }
- return FAILURE; /* Assuming nothing went wrong we never reach here */
- }
- /*
- * Delete an element from the list by username
- *
- * Parameters:
- * username The username to delete
- *
- * Return values:
- * (none)
- */
- void delete_user(char *username)
- {
- struct linked_element_s *roving = get_head()->next;
- if (!roving) {
- /* List is empty */
- return;
- }
- /*
- * Check for the special condition where the first element is for deletion
- * We do this as we have a different structure for the head than the elements
- * otherwise we could do it in a more generic fashion
- */
- if (!strcmp(roving->name, username)) {
- /* We delete first element */
- get_head()->next = roving->next; /* Remove from list */
- /* Delete dynamic contents then the element itself */
- free(roving->name);
- free(roving->password);
- free(roving);
- printf("Deleted: %s\n", username);
- return;
- }
- struct linked_element_s *tmp = roving; /* Record the element previous to the one we're checking in the loop below */
- for (roving = roving->next; roving; roving = roving->next) {
- if (!strcmp(roving->name, username)) {
- /* Delete from the second element onwards */
- tmp->next = roving->next;
- free(roving->name);
- free(roving->password);
- free(roving);
- printf("Deleted: %s\n", username);
- return;
- } else {
- tmp = roving; /* Keep tmp one behind roving */
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement