Fixing our linked list methods.

This commit is contained in:
Jacob Windle 2020-01-09 09:37:24 -05:00
parent cdc3a90605
commit 7d57550c06
2 changed files with 15 additions and 10 deletions

View File

@ -3,6 +3,7 @@
//
#include <stdio.h>
#include <string.h>
#include "linkedlist.h"
LinkedList *linkedlist_new() {
@ -12,6 +13,12 @@ LinkedList *linkedlist_new() {
return tmp;
}
static LinkedList *_linkedlist_new_with_data(char *data) {
LinkedList *tmp = linkedlist_new();
tmp->data = strdup(data);
return tmp;
}
/**
* Add a char * to our linked list and set up a new head.
* @param head The first node in the list.
@ -21,22 +28,20 @@ void linkedlist_add(LinkedList **head, char *data) {
// Create a pointer to the head element.
LinkedList *tmp = *head;
if (tmp->data == NULL) {
tmp->data = data;
if (tmp == NULL) {
*head = _linkedlist_new_with_data(data);
}
else {
fprintf(stderr, "Adding to the body\n");
// Iterate until we don't have a next node.
while ((tmp = tmp->next) != NULL) ;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = linkedlist_new();
tmp->next->data = data;
tmp->next = _linkedlist_new_with_data(data);
tmp->next->next = NULL;
}
}
void linkedlist_print(LinkedList *head) {
fprintf(stderr, "Entering linked list print\n");
LinkedList *tmp = head;
while (tmp != NULL) {
fprintf(stderr, "Node: %s\n", tmp->data);

6
main.c
View File

@ -102,7 +102,7 @@ void showHosts()
int read_config_file() {
// increment the refcount for tmp.
linkedlist_add(hosts, "hello.com");
linkedlist_add(&hosts, "hello.com");
}
/**
@ -140,8 +140,8 @@ void daemonize() {
int main(int argc, char **argv)
{
// Install a sigint handler to help us clean up.
linkedlist_add(hosts, "hello.com");
linkedlist_add(hosts, "heloworld.com");
linkedlist_add(&hosts, "hello.com");
linkedlist_add(&hosts, "heloworld.com");
linkedlist_print(hosts);
signal(SIGINT, int_handler);
if (getuid() != 0)