Thread: double queue
View Single Post
  #1 (permalink)  
Old March 31st, 2004, 01:17 AM
mafuka mafuka is offline
Registered User
 
Join Date: Mar 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default double queue

hi everyone, so i have this code for a queue or FIFO and i need to know what changes should I make so it would run as a circular or as a double queue. hope you can help me

#include <stdio.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(); /* Display the menu */

          printf( "? " );

          scanf( "%d", &choice );

           while ( choice != 3 )

            {

                   switch( choice )

                  {

                      case 1:

                                   printf( "Insert a char: " );

                                     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 = ( *headPtr )->data;

           tempPtr = *headPtr;

          *headPtr = ( *headPtr )->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" );

    }

  }

Reply With Quote