Maintaining queues

This is a discussion on Maintaining queues within the C Programming forums, part of the General Programming Boards category; I have tried to run a simple program that allows a user to add self referencing data stuctures into a ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Post Maintaining queues

    I have tried to run a simple program that allows a user to add self referencing data stuctures into a queue and then delete them from the queue.

    Whenever I try to compile it in Visual C++ i get the following error:

    error C2440: '=' : cannot convert from 'void *' to 'struct queueNode *'

    I took it straight from a book "C: How to program" by Deitel & Deitel.

    I don't know how to correct the problem, does anyone?
    If so please let me know because it's doing my brain in!

    If you manage to compile it, could you please let me know what compiler you are using.

    I'd really appreciate any suggestions.

    Cheers!

    Source code for queue.cpp:

    #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);

    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("\nEnter your choice:\n"
    " 1 to add an item to the queue\n"
    " 2 to remove an item from the queue\n"
    " 3 to end\n");

    }




    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");
    }
    }
    Last edited by dcullen99; 03-27-2002 at 05:26 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    I tried to compile it and had no problems and dont have vc++ installed so bit hard to figure out exact problem.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    newPtr = malloc(sizeof(QUEUENODE));
    malloc returns a void *
    some compiles have stricter type checking so you need to cast it

    replace the above line with:
    newPtr = (QUEUENODEPTR)malloc(sizeof(QUEUENODE));

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    some compiles have stricter type checking so you need to cast it
    Usually this happens if you're using a C++ compiler. C++ requires type casting of void pointers.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. queues
    By rebelfirst in forum C++ Programming
    Replies: 9
    Last Post: 12-02-2007, 04:33 AM
  2. Concatenating two static queues?
    By difficult.name in forum C Programming
    Replies: 2
    Last Post: 10-18-2004, 11:19 PM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  4. queues
    By jamesb in forum C Programming
    Replies: 1
    Last Post: 04-21-2002, 08:57 PM
  5. queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21