Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

9 - Dynamicky alokované pole 2/2

2D pole v dynamické paměti

#include <stdio.h>
#include <stdlib.h>
 
/** @brief Funkce pro naplneni 2D pole alokovaneho v dynamicke pameti
 *
 *  @param data
 *  @param w
 *  @param h 
 */
void fill_heap (int ** data, int w, int h)
{
    int k = 0;
 
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            data[i][j] = k++;
        }
    }
}
 
/** @brief Funkce pro tisk obsahu 2D pole alokovaneho v dynamicke pameti
 *
 *  @param data
 *  @param w
 *  @param h
 */
void print_heap (int ** data, int w, int h)
{
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            printf ("%2i ", data[i][j]);
        }
        printf("\n");
    }
}
 
/** @brief Funkce pro tisk obsahu 2D pole alokovaneho v zasobniku
 * 
 *  Pro srovnani s predchozi funkci je uvedena funkce se stejnymi 
 *  parametry. 
 *
 *  @param data
 *  @param w
 *  @param h
 */ 
void print_stack (int ** data, int w, int h)
{
    int (* q)[w] = (int(*)[w])data;
 
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            printf("%i ", q[i][j]);
        }
    }
    printf("\n");
}
 
int main ()
{
    /** Pole ukazatelu do dynamicke pameti
      * Varianta A: pole ukazatelu alokovane v zasobniku
      */ 
    int * p1[2];
    p1[0] = (int *)malloc(3*sizeof(int));
    p1[1] = (int *)malloc(3*sizeof(int));
 
    fill_heap (p1, 3, 2);
    print_heap (p1, 3, 2);
 
    /** Dealokace
      */ 
    free(p1[0]);
    free(p1[1]);
 
    /** Pole ukazatelu do dynamicke pameti
      * Varianta B: pole alokovane take v dynamicke pameti
      */  
    int ** p2 = malloc(2*sizeof(int *));
    p2[0] = malloc(3*sizeof(int));
    p2[1] = malloc(3*sizeof(int));
 
    fill_heap (p2, 3, 2);
    print_heap (p2, 3, 2);
 
    // dealokace
    free(p2[0]);
    free(p2[1]);
    free(p2);
 
 
 
}

Dynamicky vytvářený seznam vektorů (1D polí)

#include <stdio.h>
#include <stdlib.h>
 
/*
    list: | * | * | * | * |
            ^
            pole - size     informace o velikosti
                 - * data   ukazatel do d. pameti, kde jsou ulozenan data
*/
 
typedef struct 
{
    int size;
    int * data;
} array;
 
typedef struct 
{
    int size;
    array ** data;
} list;
 
array * array_init (int size)
{
    array * tmp = malloc(sizeof(array));
    tmp->size = size;
    tmp->data = malloc(size*sizeof(int));
    for (int i = 0; i < size; i++) tmp->data[i] = i;
    return tmp;
}
 
void list_init (list ** x)
{
    *x = malloc(sizeof(list));
    (*x)->size = 0;
    (*x)->data = malloc(0);
}
 
void list_deinit (list * x)
{
    for (int i = 0; i < x->size; i++)
    {
        free(x->data[i]->data);
        free(x->data[i]);
    }
    free(x->data);
    free(x);
}
 
void list_add (list * x, int size)
{
    x->data = realloc(x->data, (x->size+1)*sizeof(array *));
    x->data[x->size++] = array_init(size);
}
 
list_item_add (list * x, int position, int constant)
{
    for (int i = 0; i < x->data[position]->size; i++)
    {
        x->data[position]->data[i] += constant;
    }
}
 
void list_print (list * x)
{
    for (int i = 0; i < x->size; i++)
    {
        array * tmp = x->data[i];
        for (int j = 0; j < tmp->size; j++)
        {
            printf("%3i ", tmp->data[j]);
        }
        printf("\n");
    }
    printf("-------------------------------\n");
}
 
void list_bubble (list * x)
{
    for (int j = 0; j < x->size; j++)
    {
        for (int i = 0; i < x->size-1; i++)
        {
            if (x->data[i]->size > x->data[i+1]->size)
            {
                array * tmp = x->data[i];
                x->data[i] = x->data[i+1];
                x->data[i+1] = tmp;
            }
        }
    }
}
 
int main()
{
    list * seznam;
 
    list_init (&seznam);
    list_add (seznam, 12);
    list_add (seznam, 10);
    list_add (seznam, 4);
    list_add (seznam, 20);
    list_print (seznam);
    list_bubble (seznam);
    list_print (seznam);
    list_item_add (seznam, 1, 10);
    list_print (seznam);
 
    printf("seznam[2][10] = %i\n", seznam->data[2]->data[10]);
 
    list_deinit(seznam);
 
    return 0;
}

courses/b0b99prpa/solutions/lab09.txt · Last modified: 2021/11/30 21:56 by viteks