User Tools

Site Tools


info:teaching:pprog15

This is an old revision of the document!


Parallele Programmierung SS15

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.1430398555.txt.gz · Last modified: 2015/04/30 14:55 by moritz

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki