gdsl  1.8
examples/main_heap.c

This is an example of how to use gdsl_heap module.

/*
 * This file is part of the Generic Data Structures Library (GDSL).
 * Copyright (C) 1998-2017 Nicolas Darnis <ndarnis@free.fr>.
 *
 * GDSL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GDSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GDSL.  If not, see <http://www.gnu.org/licenses/>. 
 *
 * $RCSfile: main_heap.c,v $
 * $Revision: 1.13 $
 * $Date: 2015/02/17 12:33:16 $
 */


#include <config.h>


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


#include "gdsl_types.h"
#include "gdsl_heap.h"


#include "_integers.h"


static int 
my_display_integer (const gdsl_element_t e, gdsl_location_t location, 
            void* user_infos)
{
    printf ("%s%s%ld ", 
        (location & GDSL_LOCATION_ROOT) ? "[root]: " : "",
        (location & GDSL_LOCATION_LEAF) ? "[leaf]: " : "",
        *(long int*) e);
    return GDSL_MAP_CONT;
}

int main (void)
{
    int choix = 0;
    gdsl_heap_t h = gdsl_heap_alloc ("H", alloc_integer, free_integer, compare_integers);

    do
    {
        printf ("\t\tMENU - HEAP\n\n");
        printf ("\t1> Push: insert an element\n");
        printf ("\t2> Pop: remove max element\n");
        printf ("\t3> Get: peek max element\n");
        printf ("\t4> Set: substitute max element\n");
        printf ("\t5> Flush\n");
        printf ("\t6> Remove: *** NOT YET IMPLEMENTED ***\n");
        printf ("\t7> Display\n");
        printf ("\t8> Dump\n");
        printf ("\t9> XML display\n");
        printf ("\t0> Quit\n\n");
        printf ("\t\tYour choice: ");
        scanf ("%d", &choix);

        switch (choix)
        {
        case 1:
            {
            int value;

            printf ("Enter integer value: ");
            scanf ("%d", &value);
            gdsl_heap_insert (h, (void*) &value);
            }
            break;

        case 2:
            if (!gdsl_heap_is_empty (h))
            {
                gdsl_heap_delete_top (h);
            }
            else
            {
                printf ("The heap '%s' is empty\n", gdsl_heap_get_name (h));
            }
            break;

        case 3:
            {
            long int* top;

            if (!gdsl_heap_is_empty (h)) 
                {
                top = (long int*) gdsl_heap_get_top (h);
                printf ("Value = %ld\n", *top);
                }
            else
                {
                printf ("The heap '%s' is empty\n", gdsl_heap_get_name (h));
                }
            }
            break;

        case 4:
            {
            int value;
            long int* v;

            printf ("Enter integer value: ");
            scanf ("%d", &value);
            v = (long int*) gdsl_heap_set_top (h, (void*) &value);
            if (v == NULL)
                {
                printf ("value is greather than all other heap ones\n");
                }
            else
                {
                printf ("old value was: %ld\n", *v);
                free_integer (v);
                }
            }
            break;

        case 5:
            if (gdsl_heap_is_empty (h))
            {
                printf ("The heap '%s' is empty\n", gdsl_heap_get_name (h));
            }
            else
            {
                gdsl_heap_flush (h);
            }
            break;
            /*
        case 6:
            {
            int pos;
            long int* value;
            printf ("Enter an integer value to search: ");
            scanf ("%d", &pos);

            value = (long int*) gdsl_heap_remove (h, &pos);
            if (value == NULL)
                {
                printf ("Not found\n");
                }
            else
                {
                printf ("Value removed %ld\n", *value);
                free_integer (value);
                }
            }
            break;
            */
        case 7:
            if (gdsl_heap_is_empty (h))
            {
                printf ("The heap '%s' is empty\n", gdsl_heap_get_name (h));
            }
            else
            {
                printf ("%s = ( ", gdsl_heap_get_name (h));
                gdsl_heap_map_forward (h, my_display_integer, NULL);
                printf (")\n");
            }
            break;

        case 8:
            gdsl_heap_dump (h, print_integer, stdout, NULL);
            break;

        case 9:
            gdsl_heap_write_xml (h, print_integer, stdout, NULL);
            break;
        }
    } 
    while (choix != 0);

    gdsl_heap_free (h);

    exit (EXIT_SUCCESS);
}