Skip to content

Instantly share code, notes, and snippets.

@PurpleMyst
Created March 4, 2018 21:11
Show Gist options
  • Save PurpleMyst/939c40a7dcc0316268a209e226430ce7 to your computer and use it in GitHub Desktop.
Save PurpleMyst/939c40a7dcc0316268a209e226430ce7 to your computer and use it in GitHub Desktop.
A Linked List written in C.
#include <stdio.h>
#include <stdlib.h>
typedef void (*printer_func)(void*);
struct LinkedList {
void *value;
struct LinkedList *next;
};
struct LinkedList *linkedlist_new(void *values, size_t memb_size, size_t nmemb) {
if (nmemb < 1) {
printf("Can't create an empty Linked List.");
exit(EXIT_FAILURE);
}
struct LinkedList *root = malloc(sizeof(struct LinkedList));
root->value = values;
root->next = NULL;
if (nmemb > 1) {
struct LinkedList *conductor = root;
for (size_t i = 1; i <= nmemb; ++i) {
struct LinkedList *next = malloc(sizeof(struct LinkedList));
values = values + memb_size;
next->value = values;
conductor->next = next;
conductor = next;
}
}
return root;
}
void linkedlist_free(struct LinkedList *root) {
if (root->next != NULL) linkedlist_free(root->next);
free(root);
}
struct LinkedList *linkedlist_last(struct LinkedList *conductor) {
while (conductor->next != NULL) conductor = conductor->next;
return conductor;
}
void linkedlist_append(struct LinkedList *first, struct LinkedList *second) {
first = linkedlist_last(first);
first->next = second;
}
void linkedlist_map(struct LinkedList *conductor, printer_func printer) {
while (conductor->next != NULL) {
printer(conductor->value);
conductor = conductor->next;
}
}
void print_int(void *ptr) {
printf("%d\n", *((int*) ptr));
}
int main(void) {
const int arr[] = {1, 2, 3, 4, 5};
struct LinkedList *list = linkedlist_new((void*) arr, sizeof arr[0], sizeof arr / sizeof arr[0]);
linkedlist_map(list, print_int);
linkedlist_free(list);
}
@carlbordum
Copy link

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>


struct linkedlist;


typedef struct linkedlist
{
    int64_t value;
    struct linkedlist *next;
} linkedlist;


linkedlist *
new (int64_t i)
{
    linkedlist *x = malloc(sizeof(linkedlist));
    x->value = i;
    x->next = NULL;
    return x;
}


linkedlist *
last (linkedlist *list)
{
    linkedlist *x;
    x = list;
    while (x->next)
        x = x->next;
    return x;
}


void
append (linkedlist *list_a, linkedlist *b)
{
    linkedlist *last_in_a = last(list_a);
    last_in_a->next = b;    
}


void
print_list (linkedlist *list)
{
    putchar('[');

    linkedlist *x = list;
    while (1) {
        printf("%" PRId64, x->value);
        if (x->next) {
            putchar(',');
            putchar(' ');
        } else break;
        x = x->next;
    }

    putchar(']');
    putchar('\n');
}


int
main ()
{
    linkedlist *root = new(-1);
    for (int i = 0; i < 3; i++) {
        linkedlist *x = new(i);
        append(root, x);
    }
    print_list(root);

    linkedlist *root2 = new(8);
    for (int i = 9; i < 13; i++) {
        linkedlist *x = new(i);
        append(root2, x);
    }
    print_list(root2);

    append(root, root2);
    print_list(root);
}

@carlbordum
Copy link

carlbordum commented Mar 4, 2018

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>


struct linkedlist;


typedef struct linkedlist
{
    int64_t value;
    struct linkedlist *next;
} linkedlist;


linkedlist *
llist_new (int64_t i)
{
    linkedlist *x = malloc(sizeof(linkedlist));
    x->value = i;
    x->next = NULL;
    return x;
}


linkedlist *
llist_last (linkedlist *list)
{
    while (list->next)
        list = list->next;
    return list;
}


void
llist_append (linkedlist *list_a, linkedlist *list_b)
{
    llist_last(list_a)->next = list_b;
}


void
llist_print (linkedlist *list)
{
    putchar('[');

    linkedlist *x = list;
    while (1) {
        printf("%" PRId64, x->value);
        if (x->next) {
            putchar(',');
            putchar(' ');
        } else break;
        x = x->next;
    }

    putchar(']');
    putchar('\n');
}


int
main ()
{
    linkedlist *root = llist_new(-1);
    for (int i = 0; i < 3; i++) {
        linkedlist *x = llist_new(i);
        llist_append(root, x);
    }
    print_list(root);

    linkedlist *root2 = llist_new(8);
    for (int i = 9; i < 13; i++) {
        linkedlist *x = llist_new(i);
        llist_append(root2, x);
    }
    llist_print(root2);

    llist_append(root, root2);
    llist_print(root);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment