Fixing our linked list methods.
This commit is contained in:
parent
cdc3a90605
commit
7d57550c06
19
linkedlist.c
19
linkedlist.c
@ -3,6 +3,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include "linkedlist.h"
|
#include "linkedlist.h"
|
||||||
|
|
||||||
LinkedList *linkedlist_new() {
|
LinkedList *linkedlist_new() {
|
||||||
@ -12,6 +13,12 @@ LinkedList *linkedlist_new() {
|
|||||||
return tmp;
|
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.
|
* Add a char * to our linked list and set up a new head.
|
||||||
* @param head The first node in the list.
|
* @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.
|
// Create a pointer to the head element.
|
||||||
LinkedList *tmp = *head;
|
LinkedList *tmp = *head;
|
||||||
|
|
||||||
if (tmp->data == NULL) {
|
if (tmp == NULL) {
|
||||||
tmp->data = data;
|
*head = _linkedlist_new_with_data(data);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fprintf(stderr, "Adding to the body\n");
|
|
||||||
// Iterate until we don't have a next node.
|
// 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 = _linkedlist_new_with_data(data);
|
||||||
tmp->next->data = data;
|
|
||||||
tmp->next->next = NULL;
|
tmp->next->next = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void linkedlist_print(LinkedList *head) {
|
void linkedlist_print(LinkedList *head) {
|
||||||
fprintf(stderr, "Entering linked list print\n");
|
|
||||||
LinkedList *tmp = head;
|
LinkedList *tmp = head;
|
||||||
while (tmp != NULL) {
|
while (tmp != NULL) {
|
||||||
fprintf(stderr, "Node: %s\n", tmp->data);
|
fprintf(stderr, "Node: %s\n", tmp->data);
|
||||||
|
6
main.c
6
main.c
@ -102,7 +102,7 @@ void showHosts()
|
|||||||
int read_config_file() {
|
int read_config_file() {
|
||||||
// increment the refcount for tmp.
|
// 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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
// Install a sigint handler to help us clean up.
|
// Install a sigint handler to help us clean up.
|
||||||
linkedlist_add(hosts, "hello.com");
|
linkedlist_add(&hosts, "hello.com");
|
||||||
linkedlist_add(hosts, "heloworld.com");
|
linkedlist_add(&hosts, "heloworld.com");
|
||||||
linkedlist_print(hosts);
|
linkedlist_print(hosts);
|
||||||
signal(SIGINT, int_handler);
|
signal(SIGINT, int_handler);
|
||||||
if (getuid() != 0)
|
if (getuid() != 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user