gdsl  1.8
examples/main_queue.c

This is an example of how to use gdsl_queue module.

/*
 * This file is part of the Generic Data Structures Library (GDSL).
 * Copyright (C) 1998-2018 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/>. 
 *
 *
 * GDSL - Generic Data Structures Library
 * $RCSfile: main_queue.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_queue.h"


#include "_integers.h"


static void
my_write_integer (gdsl_element_t e, FILE* file, gdsl_location_t location, void* d)
{
    int value = * (int*) e;

    if (location & GDSL_LOCATION_HEAD)
    {
        fprintf (file, "( %d", value);
    }
    else
    {
        fprintf (file, " %d", value);
    }

    if (location & GDSL_LOCATION_TAIL)
    {
        fprintf (file, " )\n");
    }
}

static int 
my_display_integer (gdsl_element_t e, gdsl_location_t location, void* d)
{
    my_write_integer (e, stdout, location, d);
    return GDSL_MAP_CONT;
}

int main (void)
{
    int choice = 0;
    gdsl_queue_t q = gdsl_queue_alloc ("Q", alloc_integer, free_integer);
  
    do
    {
        printf ("\t\tMENU - QUEUE\n\n");
        printf ("\t1> Put\n");
        printf ("\t2> Pop\n");
        printf ("\t3> Get Head\n");
        printf ("\t4> Get Tail\n");
        printf ("\t5> Flush\n");
        printf ("\t6> Search\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", &choice );

        switch (choice)
        {
        case 1:
            {
            int value;
            printf ("Enter an integer value: ");
            scanf ("%d", &value);
            gdsl_queue_insert (q, (void*) &value);
            }
            break;

        case 2:
            if (!gdsl_queue_is_empty (q))
            {
                int* value = (int*) gdsl_queue_remove (q);
                printf ("Value: %d\n", *value);
                free_integer (value);
            }
            else
            {
                printf ("The queue '%s' is empty\n", gdsl_queue_get_name (q));
            }
            break;
      
        case 3:
            {
            if (!gdsl_queue_is_empty (q)) 
                {
                int head = *(int*) gdsl_queue_get_head (q);
                printf ("Head = %d\n", head);
                }
            else
                {
                printf ("The queue '%s' is empty\n", gdsl_queue_get_name (q));
                }
            }
            break;

        case 4:
            {
            if (!gdsl_queue_is_empty (q)) 
                {
                int tail = *(int*) gdsl_queue_get_tail (q);
                printf ("Tail = %d\n", tail);
                }
            else
                {
                printf ("The queue '%s' is empty\n", gdsl_queue_get_name (q));
                }
            }
            break;

        case 5:
            if (gdsl_queue_is_empty (q))
            {
                printf ("The queue '%s' is empty\n", gdsl_queue_get_name (q));
            }
            else
            {
                gdsl_queue_flush (q);
            }
            break;

        case 6:
            {
            int pos;
            int* value;
            printf ("Enter an integer value to search an element by its position: ");
            scanf ("%d", &pos);

            value = (int*) gdsl_queue_search_by_position (q, pos);
            if (value != NULL)
                {
                printf ("Value found at position %d = %d\n", pos, *value);
                }
            }
            break;

        case 7:
            if (gdsl_queue_is_empty (q))
            {
                printf ("The queue '%s' is empty\n", gdsl_queue_get_name (q));
            }
            else
            {
                printf ("%s = ", gdsl_queue_get_name (q));
                gdsl_queue_map_forward (q, my_display_integer, NULL);
            }
            break;

        case 8:
            gdsl_queue_dump (q, my_write_integer, stdout, NULL);
            break;

        case 9:
            gdsl_queue_write_xml (q, my_write_integer, stdout, NULL);
            break;
        }
    } 
    while (choice != 0);

    gdsl_queue_free (q);

    exit (EXIT_SUCCESS);
}