Created
August 18, 2019 05:04
-
-
Save quoll/5a0e34899e217e384eb1949893b311c9 to your computer and use it in GitHub Desktop.
This file contains 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> | |
struct element { | |
long next; | |
long value; | |
}; | |
void print_list(void* memory, long list_id) { | |
while (list_id != -1) { | |
struct element* list = memory + list_id * sizeof(struct element); | |
printf("%d", list->value); | |
list_id = list->next; | |
if (list_id != -1) { | |
printf(", "); | |
} | |
} | |
printf("\n"); | |
} | |
long init_element(void* memory, long new_head_id, long list_id, long 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)); | |
long list_id = init_element(memory, 0, -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