IRCWebHooks
Barry Adding files 3f04e22 (3 years, 3 months ago)diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..54e1cff --- /dev/null +++ b/src/list.c @@ -0,0 +1,89 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdbool.h> + +#include "list.h" +#include "irc.h" + +/* Insert first item */ +void +insert_first(char *data) +{ + struct Node *link = (struct Node*) malloc(sizeof(struct Node)); + strcpy(link->data, data); + link->next = head; + head = link; +} + +/* Delete the first item */ +struct Node * +delete_first(void) +{ + struct Node *tempLink = head; + head = head->next; + free(tempLink); + return tempLink; +} + +/* Check if list is empty */ +bool +is_empty(void) +{ + return head == NULL; +} + +int +length(void) +{ + int length = 0; + struct Node *current; + for(current = head; current != NULL; current = current->next) { + length++; + } + return length; +} + +/* Find Node with data */ +struct Node * +find(char *data) +{ + struct Node* current = head; + if (head == NULL) { + return NULL; + } + while (strncmp(current->data, data, strlen(data))) { + if (current->next == NULL) { + return NULL; + } else { + current = current->next; + } + } + return current; +} + +/* Delete a Node */ +struct Node * +delete(char *data) +{ + struct Node* current = head; + struct Node* previous = NULL; + if (head == NULL) { + return NULL; + } + while (strncmp(current->data, data, strlen(data))) { + if(current->next == NULL) { + return NULL; + } else { + previous = current; + current = current->next; + } + } + if (current == head) { + head = head->next; + } else { + previous->next = current->next; + } + free(current); + return current; +}