Skip to content

Instantly share code, notes, and snippets.

@quoll
Created August 18, 2019 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quoll/8510338ea4283585099a862fb9962a76 to your computer and use it in GitHub Desktop.
Save quoll/8510338ea4283585099a862fb9962a76 to your computer and use it in GitHub Desktop.
Linked list with bytes
#include <stdio.h>
#include <stdlib.h>
struct element {
unsigned char next;
unsigned char value;
};
void print_list(void* memory, unsigned char list_id) {
while (list_id != (unsigned char)-1) {
struct element* list = memory + list_id * sizeof(struct element);
printf("%d", list->value);
list_id = list->next;
if (list_id != (unsigned char)-1) {
printf(", ");
}
}
printf("\n");
}
unsigned char init_element(void* memory, unsigned char new_head_id, unsigned char list_id, unsigned char value) {
struct element* new_head = memory + new_head_id * sizeof(struct element);
new_head->next = list_id;
new_head->value = value;
return new_head_id;
}
int main(int argc, char** argv) {
void* memory = malloc(10 * sizeof(struct element));
unsigned char list_id = init_element(memory, 0, (unsigned char)-1, 34);
list_id = init_element(memory, 1, list_id, 21);
list_id = init_element(memory, 2, list_id, 13);
list_id = init_element(memory, 3, list_id, 8);
list_id = init_element(memory, 4, list_id, 5);
list_id = init_element(memory, 5, list_id, 3);
list_id = init_element(memory, 6, list_id, 2);
list_id = init_element(memory, 7, list_id, 1);
list_id = init_element(memory, 8, list_id, 1);
print_list(memory, list_id);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment