hey guy i'm having some dificulties in trying to convert this FIFO into a LIFO, what i've done so far is that the program tells me that the char that i have eliminated is the last one but then the whole stack is empty, so i would really apreciate some pointers :D
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct queueNode
{
char data;
struct queueNode *nextPtr;
};
typedef struct queueNode QueueNode;
typedef QueueNode *QueueNodePtr;
void printQueue( QueueNodePtr );
int isEmpty( QueueNodePtr );
char dequeue( QueueNodePtr *, QueueNodePtr * );
void enqueue( QueueNodePtr *, QueueNodePtr *, char );
void instructions( void );
int main()
{
QueueNodePtr headPtr = NULL, tailPtr = NULL;
int choice;
char item;
instructions();
printf( "? " );
scanf( "%d", &choice );
while ( choice != 3 )
{
switch( choice )
{
case 1:
printf( "Introduzca un Caracter: " );
scanf( "\n%c", &item );
enqueue( &headPtr, &tailPtr, item );
printQueue( headPtr );
break;
case 2:
if ( !isEmpty( headPtr ) )
{
item = dequeue( &headPtr, &tailPtr );
printf( "%c a Sido Eliminado.\n", item );
}
printQueue( headPtr );
break;
default:
printf( "Opcion Invalida.\n\n" );
instructions();
break;
}
printf( "? " );
scanf( "%d", &choice );
}
printf( "Fin de Corrida.\n" );
return 0;
}
void instructions( void )
{
printf ( "Introduzca su Opcion:\n"
" 1 Para Insertar un Elemento a la Cola\n"
" 2 Para Eliminar un Elemento de la Cola\n"
" 3 Para Salir\n" );
}
void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value )
{
QueueNodePtr newPtr;
newPtr = new QueueNode;
if ( newPtr != NULL )
{
newPtr->data = value;
newPtr->nextPtr = NULL;
if ( isEmpty( *headPtr ) )
*headPtr = newPtr;
else
( *tailPtr )->nextPtr = newPtr;
*tailPtr = newPtr;
}
else
printf( "%c No Insertado. No Memoria Disponible.\n", value );
}
char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr )
{
char value;
QueueNodePtr tempPtr;
value = ( *tailPtr )->data;
tempPtr = *headPtr;
*headPtr = ( *tailPtr )->nextPtr;
if ( *headPtr == NULL )
*tailPtr = NULL;
free( tempPtr );
return value;
}
int isEmpty( QueueNodePtr headPtr )
{
return headPtr == NULL;
}
void printQueue( QueueNodePtr currentPtr )
{
if ( currentPtr == NULL )
printf( "La Cola esta Vacia.\n\n" );
else
{
printf( "La Cola Es:\n" );
while ( currentPtr != NULL )
{
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}
printf( "NULL\n\n" );
}
}