[c] Algorytm LIFO/FIFO
: 04 grudnia 2006, 18:59
Kurcze mam sobie taki algorytm w C i coś nie bardzo chce mi zadziałać tak jak chcę:
nie bardzo wiem gdzie mam błąd, może ktoś wypatrzy go bo ja go nie widzę...
Kod: Zaznacz cały
// LIFO
#include <stdio.h>
#include <stdlib.h>
struct Wezel {
int value;
struct Wezel *next, *prev;
};
struct Stos {
struct Wezel *top, *low;
};
void stos_new( struct Stos *s );
int push( struct Stos *s );
void pop( struct Stos *s, int val );
void stos_del( struct Stos *s );
//-------------------------------------------------
void main()
{
struct Stos *stos = malloc( sizeof( struct Stos ));
stos_new( stos );
pop( stos, 1 );
pop( stos, 12 );
pop( stos, 13 );
pop( stos, 14 );
pop( stos, 15 );
pop( stos, 16 );
printf("\n");
while( stos->top != 0 );
{
printf("%d ", push( stos ) );
}
printf("\n");
stos_del( stos );
}
//-------------------------------------------------
void stos_new( struct Stos *s ) {
s->low = 0;
s->top = 0;
}
//-------------------------------------------------
int push( struct Stos *s )
{
int val = s->top->value;
struct Wezel *stary = s->top;
s->top = s->top->prev;
free( stary );
return val;
}
//-------------------------------------------------
void pop( struct Stos *s, int val )
{
struct Wezel *nowy = malloc( sizeof( struct Wezel ) );
nowy->value = val;
if( s->top != 0 )
{
s->top->next = nowy;
nowy->prev = s->top;
}
else
{
s->low = nowy;
nowy->prev = 0;
}
s->top = nowy;
}
void stos_del( struct Stos *s )
{
struct Wezel *temp;
struct Wezel *check = s->top;
while( check != 0 )
{
temp = check;
check = s->top->prev;
free( temp );
}
free( s );
}