Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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 19th, 2004, 02:20 AM
Registered User
 
Join Date: Mar 2004
Location: nogales, sonora, Mexico.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default FIFO

hi everyone, so i've been busting my head open with this code, it marks to errors i just can't seem to fix them, so could anyone give it a shot?

/* Operating and maintaining a queue */

#include <stdio.h>
#include <stdlib.h>
struct queueNode { /* self-referential stucture */
   char data;
   struct queueNode *nextPtr;
};
typedef struct queueNode QUEUENODE;
typedef QUEUENODE *QUEUENODEPTR;

/* function prototypes */
void printQueue(QUEUENODEPTR);
int isEmpty(QUEUENODEPTR);
char dequeue(QUEUENODEPTR *, QUEUENODEPTR *);
void enqueue(QUEUENODEPTR *, QUEUENODEPTR *, char);

main()
{
   QUEUENODEPTR headPtr = NULL, tailPtr = NULL;
   int choice;
   char item;

   instructions();
   printf("? ");
   scanf("%d", &choice);

   while (choice != 3) {

      switch(choice) {

         case 1:
            printf("Enter a character: ");
            scanf("\n%c", &item);
            enqueue(&headPtr, &tailPtr, item);
            printQueue(headPtr);
            break;

         case 2:
            if (!isEmpty(headPtr)) {
               item = dequeue(&headPtr, &tailPtr);
               printf("%c has been dequeued.\n", item);
            }

            printQueue(headPtr);
            break;

         default:
            printf("Invalid choice.\n\n");
            instructions();
            break;
      }

      printf("? ");
      scanf("%d", &choice);
   }

   printf("End of run.\n");
   return 0;
}

void instructions(void)
{
   printf ("Enter your choice:"
           " 1 to add an item to the queue"
           " 2 to remove an item from the queue"
           " 3 to end");
}

void enqueue(QUEUENODEPTR *headPtr, QUEUENODEPTR *tailPtr,
             char value)
{
   QUEUENODEPTR newPtr;

   newPtr = malloc(sizeof(QUEUENODE));

   if (newPtr != NULL) {
      newPtr->data = value;
      newPtr->nextPtr = NULL;

      if (isEmpty(*headPtr))
         *headPtr = newPtr;
      else
         (*tailPtr)->nextPtr = newPtr;

      *tailPtr = newPtr;
   }
   else
      printf("%c not inserted. No memory available.\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("Queue is empty.\n\n");
   else {
      printf("The queue is:\n");

      while (currentPtr != NULL) {
         printf("%c --> ", currentPtr->data);
         currentPtr = currentPtr->nextPtr;
      }

      printf("NULL\n\n");
   }
}
Reply With Quote
  #2 (permalink)  
Old March 19th, 2004, 05:19 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe if you include what the error messages are, someone could more easily pinpoint the problem.

Gert

Reply With Quote
  #3 (permalink)  
Old March 20th, 2004, 03:32 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Did your compiler complain about your function instructions()? Sometimes compiler messages are somewhat cryptic (to the uninitiated, and here's your initiation).

Anyhow, when you use a function and you haven't told C anything about it, C assumes that it is a function that returns an int, and all of its arguments (if any) are ints. Does this make the compiler warnings/errors more clear?

Put a prototype of instructions() before it is invoked.

Then see if there are any other compiler messages.

No?, then on to testing/debugging.

Good luck,

Dave
Reply With Quote
  #4 (permalink)  
Old March 22nd, 2004, 03:39 AM
Registered User
 
Join Date: Mar 2004
Location: nogales, sonora, Mexico.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok the errors are:
Info :Compiling C:\Documents and Settings\Guezt\My Documents\cola.cpp
Error: fifo.cpp(24,17):Call to undefined function 'instructions'
Error: fifo.cpp(75,12):Cannot convert 'void *' to 'queueNode *'


Reply With Quote
  #5 (permalink)  
Old March 26th, 2004, 12:34 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:
 ok the errors are:
Info :Compiling C:\Documents and Settings\Guezt\My Documents\cola.cpp
Error: fifo.cpp(24,17):Call to undefined function 'instructions'
Error: fifo.cpp(75,12):Cannot convert 'void *' to 'queueNode *'
To eliminate the first error, place the following with your function prototypes:

Code:
    void instructions(void);

To eliminate the second error, you can change the malloc() statement to
Code:
   newPtr = (QUEUENODEPTR)malloc(sizeof(QUEUENODE));
Dave
Reply With Quote




Similar Threads
Thread Thread Starter Forum Replies Last Post
FIFO 2 LIFO mafuka C++ Programming 6 May 13th, 2004 06:44 PM





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