Last active
August 6, 2024 02:32
-
-
Save assyrianic/cdc29c488071f91f8540794c3b6e0be9 to your computer and use it in GitHub Desktop.
a dumb linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <time.h> | |
struct Node { | |
struct Node *prev, *next; | |
int data; | |
}; | |
struct Node *node_new(int const data) { | |
struct Node *n = malloc(sizeof *n); | |
n->prev = n->next = NULL; | |
n->data = data; | |
return n; | |
} | |
void node_cleanup(struct Node **n_ref) { | |
if( n_ref==NULL || *n_ref==NULL ) { | |
return; | |
} | |
for( struct Node *n = *n_ref, *m = NULL; n != NULL; n = m ) { | |
m = n->next; | |
free(n); | |
} | |
*n_ref = NULL; | |
} | |
struct LList { | |
struct Node *head, *tail; | |
size_t len; | |
}; | |
void llist_append_node(struct LList *const l, struct Node *const n) { | |
if( l->tail==NULL ) { | |
l->head = l->tail = n; | |
} else { | |
l->tail->next = n; | |
n->prev = l->tail; | |
l->tail = n; | |
} | |
l->len++; | |
} | |
void llist_append_data(struct LList *const l, int const d) { | |
llist_append_node(l, node_new(d)); | |
} | |
void llist_prepend_node(struct LList *const l, struct Node *const n) { | |
if( l->head==NULL ) { | |
l->head = l->tail = n; | |
} else { | |
l->head->prev = n; | |
n->next = l->head; | |
l->head = n; | |
} | |
l->len++; | |
} | |
void llist_prepend_data(struct LList *const l, int const d) { | |
llist_prepend_node(l, node_new(d)); | |
} | |
void llist_insort_node(struct LList *const l, struct Node *const n) { | |
if( l->head==NULL || l->head->data < n->data ) { | |
// if larger than head, prepend, we're doing descending order. | |
llist_prepend_node(l, n); | |
return; | |
} | |
struct Node *i = l->head; | |
while( i != NULL && i->data >= n->data ) { | |
i = i->next; | |
} | |
if( i==NULL ) { | |
// if NULL, then even tail's value is larger, so append it | |
llist_append_node(l, n); | |
return; | |
} | |
n->next = i; | |
i->prev->next = n; | |
n->prev = i->prev; | |
i->prev = n; | |
l->len++; | |
} | |
void llist_insort_data(struct LList *const l, int const d) { | |
llist_insort_node(l, node_new(d)); | |
} | |
void llist_print(struct LList const *const l) { | |
for( struct Node *n = l->head; n != NULL; n=n->next ) { | |
printf("[Prev: %16p | Node Value: %16p : '%10i' | Next: %16p]\n", n->prev, n, n->data, n->next); | |
} | |
} | |
struct Node const *llist_find_data(struct LList const *const l, int const d) { | |
for( struct Node const *n = l->head; n != NULL; n=n->next ) { | |
if( n->data==d ) { | |
return n; | |
} | |
} | |
return NULL; | |
} | |
void llist_cleanup(struct LList *const l) { | |
node_cleanup(&l->head); | |
l->tail = NULL; | |
l->len = 0; | |
} | |
int main() { | |
srand(time(NULL)); | |
struct LList l = {0}; | |
for( int i = 0; i < 20; i++ ) { | |
int const a = rand() % 10000; | |
printf("a: %i\n", a); | |
llist_insort_data(&l, a); | |
} | |
llist_print(&l); | |
llist_cleanup(&l); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment