User Tools

Site Tools


info:teaching:pprog15

Parallele Programmierung SS15

Class 12

Class 10

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

struct node {
    int value;
    struct node *next;
};

void insert(struct node *list, struct node *element) {
    element->next = list->next;
    list->next = element;
}

void print(struct node *e) {
    printf("e->value=%d\n", e->value);
}

void apply(void (*f)(struct node *), struct node *list) {
    while (list) {
        f(list);
        list = list->next;
    }
}


int main()
{
    int i;
    struct node list;
    list.value = 99999;
    list.next = NULL;

    for (i = 1; i <= 10; i++) {
        struct node *element = malloc(sizeof(struct node));
        if (element == NULL) {
            return 1;
        }
        element->value = i << 2;
        insert(&list, element);
    }
    struct node *current = &list;
/*
    while (current) {
        printf("%d ", current->value);
        current = current->next;
    }
    printf("\n"); */

    apply(&print, &list);

    current = list.next;
    while (current) {
        printf("%d ", current->value);
        struct node *n = current;
        current = current->next;
        free(n);
    }

    printf("Hello world!\n");
    return 0;
}
info/teaching/pprog15.txt · Last modified: 2015/05/21 13:22 by moritz

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki