Skip to content

Instantly share code, notes, and snippets.

@parkj90
Last active July 30, 2018 22:59
Show Gist options
  • Save parkj90/de7b1873babeb606c61ab3285889acbf to your computer and use it in GitHub Desktop.
Save parkj90/de7b1873babeb606c61ab3285889acbf to your computer and use it in GitHub Desktop.
linked list
//list.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
static void print(int value);
list_t *list_insert(list_t *list, int value) {
list_t *node = malloc(sizeof(list_t));
if (node == NULL) {
return NULL;
}
if (list == NULL) {
node->next = NULL;
} else {
node->next = list->next;
list->next = node;
}
node->data = value;
return node;
}
list_t *list_append(list_t *list, int value) {
list_t *node= malloc(sizeof(list_t));
if (node == NULL) {
return NULL;
}
if (list) {
while (list->next != NULL) {
list = list->next;
}
list->next = node;
}
node->next = NULL;
node->data = value;
return node;
}
list_t *list_search(list_t *list, int value, bool (*f)(int, int) ) {
if (list == NULL) {
return NULL;
}
while(!f(value, list->data)) {
if (list->next == NULL) {
return NULL;
}
list = list->next;
}
return list;
}
list_t *list_delete(list_t *list, list_t *node) {
if (list == NULL) {
return NULL;
}
list_t *temp;
if (list == node) {
temp = list->next;
free(list);
list = temp;
} else {
while(list->next != node) {
list = list->next;
}
temp = node->next;
free(node);
list->next = temp;
}
return list;
}
bool list_get(list_t *node, int *value) {
if (node == NULL) {
return false;
}
*value = node->data;
return true;
}
bool list_set(list_t *node, int value) {
if (node == NULL) {
return false;
}
node->data = value;
return true;
}
unsigned int list_length(list_t *list) {
unsigned int length = 0;
while (list) {
list = list->next;
length++;
}
return length;
}
void list_traverse(list_t *list, void (*f)(int)) {
if (list == NULL) {
return;
}
while (list) {
f(list->data);
list = list->next;
}
}
void list_free(list_t *list) {
list_t *temp;
while (list) {
temp = list->next;
free(list);
list = temp;
}
}
void list_dump(list_t *list) {
list_traverse(list, print);
printf("\n");
}
static void print(int value) {
printf("%d ", value);
}
//list.h
#include <stdbool.h>
typedef struct list{
struct list *next;
int data;
} list_t;
list_t *list_insert(list_t *list, int value);
list_t *list_append(list_t *list, int value);
// function args: bool f(int value, int x);
list_t *list_search(list_t *list, int value, bool (*f)(int, int) );
list_t *list_delete(list_t *list, list_t *node);
bool list_get(list_t *list, int *value);
bool list_set(list_t *list, int value);
unsigned int list_length(list_t *list);
// function arg: void f(int value);
void list_traverse(list_t *list, void (*f)(int));
void list_free(list_t *list);
void list_dump(list_t *list);
//main.c
#include <stdio.h>
#include <assert.h>
#include "list.h"
bool test_function(int value, int x);
void print_double(int value);
void list_assert(list_t *list, int values[], unsigned int length);
int main(void) {
int value;
list_t *head = list_insert(NULL, 32);
if (!head) {
perror("failed to allocate list");
return -1;
}
//Test insert
list_t *temp = list_insert(head, 10);
assert(head->next == temp);
assert(temp->next == NULL);
temp = list_insert(temp, 20);
assert(head->next->next == temp);
assert(temp->next == NULL);
assert(list_length(head) == 3);
list_assert(head, (int[]){32, 10, 20}, 3);
//Test appending from various points in the list
temp = list_append(temp, 42); //from end
assert(temp->data == 42);
temp = list_append(head, 24); //from beginning
assert(temp->data == 24);
temp = list_append(head->next, 10); //from middle
assert(temp->data == 10);
assert(list_length(head) == 6);
list_assert(head, (int[]){32, 10, 20, 42, 24, 10}, 6);
//Test delete
list_delete(head, head->next);
list_delete(head, head->next->next);
list_delete(head, head->next);
head = list_delete(head, head);
list_assert(head, (int[]){24, 10}, 2);
assert(list_length(head) == 2);
//Test set and get
list_append(head, 0);
temp = list_append(head, 0);
assert(list_set(temp, 3) == true);
assert(temp->data == 3);
assert(list_get(temp, &value) == true);
assert(value == 3);
list_assert(head, (int[]){24, 10, 0, 3}, 4);
//Test search
temp = list_search(head, 0, test_function);
assert(temp->data == 0);
temp = list_search(head, 24, test_function);
assert(temp->data == 24);
assert(list_search(head, 3, test_function)->data == 3);
assert(list_search(head, 10, test_function)->data == 10);;
list_search(head, 1, test_function);
//Test traverse
list_dump(head);
list_traverse(head, print_double); //should print 48 20 0 6
list_t *new_head = list_append(NULL, 5);
list_dump(new_head);
list_free(head);
list_free(new_head);
return 0;
}
bool test_function(int value, int x) {
if (value == x) {
return true;
}
return false;
}
void print_double(int value) {
printf("%d ", value * 2);
}
void list_assert(list_t *list, int values[], unsigned int length) {
for (int i = 0; i < length; i++) {
assert(list->data == values[i]);
list = list->next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment