Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 31st, 2004, 01:17 AM
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
  #2 (permalink)  
Old April 6th, 2004, 01:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Without looking too closely at your code, I would suggest keeping an extra pointer. One pointer will point to the front of the queue, the other to the back. This means that you can quickly perform push and pop operations at both ends.

You don't care about the stuff in the middle, because a queue (by definition) can only insert and remove at one end.

You'll end up having functions like push_front(), pop_front(), push_back(), pop_back() instead of push() and pop(). These allow the programmer using your queue to determine which end of the queue to push and pop from.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Message Queue vs. FTP andysc BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 1 April 28th, 2008 07:23 PM
Creating Queue Messaging! rosalynb VS.NET 2002/2003 2 January 28th, 2006 08:13 AM
Circular queue bulletgreen BOOK: Professional C++ 0 December 15th, 2005 04:28 AM
Array and the circular queue bidhya Java GUI 0 May 13th, 2004 02:24 AM
Printing queue Maximus Excel VBA 1 August 15th, 2003 07:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.