2013-11-27 16:28:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* This file subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup core_util
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file lifo.c
|
|
|
|
* @brief LIFO buffer implementation
|
|
|
|
*
|
2013-11-27 17:54:30 +01:00
|
|
|
* @file lifo.c
|
|
|
|
* @brief LIFO buffer implementation
|
|
|
|
* @author unknown
|
2013-11-27 16:28:31 +01:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "lifo.h"
|
2010-12-10 16:49:29 +01:00
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int lifo_empty(int *array)
|
|
|
|
{
|
2010-12-10 16:49:29 +01:00
|
|
|
return array[0] == -1;
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
void lifo_init(int *array, int n)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
for (int i = 0; i <= n; i++) {
|
2010-12-10 16:49:29 +01:00
|
|
|
array[i] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
void lifo_insert(int *array, int i)
|
|
|
|
{
|
|
|
|
int index = i + 1;
|
2010-12-10 16:49:29 +01:00
|
|
|
array[index] = array[0];
|
|
|
|
array[0] = i;
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int lifo_get(int *array)
|
|
|
|
{
|
2010-12-10 16:49:29 +01:00
|
|
|
int head = array[0];
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (head != -1) {
|
2013-06-20 18:18:29 +02:00
|
|
|
array[0] = array[head + 1];
|
2010-12-10 16:49:29 +01:00
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-12-10 16:49:29 +01:00
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_MAIN
|
|
|
|
#include <stdio.h>
|
2013-06-20 18:18:29 +02:00
|
|
|
int main()
|
|
|
|
{
|
2010-12-10 16:49:29 +01:00
|
|
|
int array[5];
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-12-10 16:49:29 +01:00
|
|
|
lifo_init(array, 4);
|
|
|
|
|
|
|
|
lifo_insert(array, 0);
|
|
|
|
lifo_insert(array, 1);
|
|
|
|
lifo_insert(array, 2);
|
|
|
|
lifo_insert(array, 3);
|
|
|
|
printf("get: %i\n", lifo_get(array));
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|