~lbnz/xr0

632555dc037a5b584bd0b2c7ba242e6453af4a9b — Xr0 Team 6 months ago 868d45a path/linked-list
linked list
2 files changed, 51 insertions(+), 0 deletions(-)

R tests/3-program/{matrix.x => 000-matrix.x}
A tests/3-program/001-linked-list.x
R tests/3-program/matrix.x => tests/3-program/000-matrix.x +0 -0
A tests/3-program/001-linked-list.x => tests/3-program/001-linked-list.x +51 -0
@@ 0,0 1,51 @@
#include <stdlib.h>
#include <stdio.h>

struct list {
	int num;
	struct list *next;
};

struct list *
list_create(int num) [
	.alloc result;
	result->num = num;
	result->next = NULL;
]{
	struct list *l = malloc(sizeof(struct list));
	l->num = num;
	l->next = NULL;
	return l;
}

void
list_destroy(struct list *l) [
	int i;

	pre: {
		l = list_create($);
		for (i = 0; i < $; i++) {
			list_append(l, $);
		}
	}

	if (l->next) {
		list_destroy(l->next);
	}
	.dealloc l;
]{
	if (l->next) {
		list_destroy(l->next);
	}
	free(l);
}

void
list_append(struct list *l, int num) [
]{
	if (l->next) {
		list_append(l->next, num);
	} else {
		l->next = list_create(num);
	}
}