domingo, 22 de noviembre de 2009

AlgoritmosMiPila

#include
#include
#include
#define MAXSTACK 10

typedef char cad[MAXSTACK];
typedef struct{
int index;
cad pila [MAXSTACK];
} stackito;

void CleanStack (stackito *st);
bool IsEmpty (stackito st);
bool IsFull (stackito st);
void Push (stackito *st, char *elem);
char *Pop (stackito *st);

main ()
{
int menu;
stackito st;
st.index = -1;
cad elem={0};
do
{
// clrscr();
printf("Seleccione la opcion deseada:\n 1. Limpiar lista\n 2. Insertar elemento\n 3. Extraer elemento\n 4. Ver ultimo elemento\n 5. Salir\n");
scanf ("%d", &menu);
switch (menu)
{
case 1:
CleanStack (&st);
break;
case 2:
printf ("Introduce el siguiente elemento: \n");
scanf ("%s", &elem);
Push (&st, elem);//(stackito *st, char elem)
break;
case 3:
if(IsEmpty(st)==true)
{
printf("Pila Vacia\n");
}
else
{
printf("Elemento estraido %s \n",(Pop(&st)));
st.index=st.index-1;
}
break;
case 4:
if(IsEmpty(st)==true)
{
printf("Pila Vacia\n");
}
else
{
printf("%s \n",(Pop(&st)));
}
break;
case 5:
break;
}
} // fin do
while (menu!=5);
} // fin main


void CleanStack (stackito *st)
{
st -> index =-1;
}

bool IsEmpty (stackito st)
{
if (st.index==-1)
{
return true;
}
return false;
}

bool IsFull (stackito st)
{
if (st.index==MAXSTACK-1)
{
return true;
}
return false;
}

void Push (stackito *st, char *elem)
{
if (IsFull(*(st)))
{
printf("Pila llena");
}
else
{
st->index=st->index+1;
for (int i = 0; i < 10; i++)
{
st->pila[st->index][i]=(elem[i]);
}
}
}

char *Pop (stackito *st)
{
if (IsEmpty (*(st)))
{
printf ("Pila vacia");
return NULL;
}
return &(st->pila[st->index][0]);
}

*Cuando hay pase por valor se usa st.index, cuando es pase por referencia es st->index
**Si tuve algun error omitir comentario y decirme por msn!

No hay comentarios:

Publicar un comentario