2010-12-10 16:49:29 +01:00
|
|
|
#include <lifo.h>
|
|
|
|
|
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
|