Thread: What is wrong with my queue ADT?

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    23

    What is wrong with my queue ADT?

    Code:
    #include "queue.h"
    
    QUEUE* CreateQueue(void)
    {
        QUEUE* queue;
    
    
        queue = (QUEUE*)malloc(sizeof(QUEUE));
    
    
        if(queue)
        {
            int count = 0;
            queue->front = NULL;
            queue->rear = NULL;
        }
        else
            return NULL;
        return queue;
    }
    
    
    bool Enqueue(QUEUE* queue, void* dataptr)
    {
        NODE* newnode;
        
        if(!(newnode = (NODE*) malloc(sizeof(NODE))))
            return FALSE;
    
    
        newnode->dataptr = dataptr;
        newnode->next = NULL;
    
    
        if(queue->count == 0)
            queue->front = newnode;
        else
            queue->rear->next = newnode;
        (queue->count)++;
        queue->rear = newnode;
        return TRUE;
    }
    
    
    void* Dequeue (QUEUE* queue)
    {
        void* dataOut;
        NODE* temp;
        
        if(queue->count == 0)
            return NULL;
        dataOut = queue->front->dataptr;
        temp = queue->front;
        queue->front = queue->front->next;
        if(queue->count == 1)
            queue->rear = NULL;
        (queue->count)--;
        free(temp);
        return dataOut;
    }
    
    
    void* QueueFront (QUEUE* queue)
    {
        void* dataOut;
        
        if(queue->count == 0)
            return NULL;
        else
            return queue->front->dataptr;
    }
    
    
    void* QueueRear (QUEUE* queue)
    {
        void* dataOut;
        if(queue->count == 0)
            return NULL;
        else
            return queue->rear->dataptr;
    }
    
    
    bool EmptyQueue (QUEUE* queue)
    {
        return (queue->count == 0);
    }    
    
    
    bool FullQueue (QUEUE* queue)
    {
        NODE* temp;
    
    
        temp = (NODE*)malloc(sizeof(*(queue->rear)));
        if(temp)
        {
            free(temp);
            return FALSE;
        }
        return TRUE;
    }
    
    
    int QueueCount(QUEUE* queue)
    {
        return queue->count;
    }
    
    
    void DestroyQueue(QUEUE* queue)
    {
        NODE* temp;
        if(queue->count != 0)
        {
            while(queue->front != NULL)
            {
                temp = queue->front;
                queue->front = queue->front->next;
                free(temp->dataptr);
                free(temp);
            }
        }
        free(queue);
    }
    So I tried to enqueue something very simple in main as so:
    Code:
    #include "queue.h"
    
    
    int main(void)
    {
        int* a;
        QUEUE* queue;
    
    
        queue = CreateQueue();
        a = (int*)malloc(sizeof(int));
        *a = 1;
        Enqueue(queue, a);
        return 0;
    }
    When I compile, the program crashes and my debugger states that there was an exception at this line:
    Code:
    bool Enqueue(QUEUE* queue, void* dataptr){
        NODE* newnode;
        
        if(!(newnode = (NODE*) malloc(sizeof(NODE))))
            return FALSE;
    
    
        newnode->dataptr = dataptr;
        newnode->next = NULL;
    
    
        if(queue->count == 0)
            queue->front = newnode;
        else
            queue->rear->next = newnode;
        (queue->count)++;
        queue->rear = newnode;
        return TRUE;
    }
    I am using Microsoft visual studio 2010.

    Any help is appreciated!
    Last edited by slee8251; 10-08-2012 at 12:30 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Your code for queue.h is what?

    Edit: A random guess of the problem; you forgot to include the header "stdlib.h".
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    There is nothing wrong with this except in the event that stdlib.h, the header which declares malloc, is not included.
    Tim S.
    Last edited by stahta01; 10-08-2012 at 12:19 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    On line 13 you are not initialising queue->count, you are instead creating and not using a separate variable then goes out of scope shortly after.
    Also, in main, everything is going okay and then you igmore your malloc'ed int and just pass in 5 directly. What's with that?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    23
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    typedef struct node
    {
        void* dataptr;
        struct node* next;
    } NODE;
    
    
    typedef struct queue
    {
        int count;
        NODE* front;
        NODE* rear;
    } QUEUE;
    
    
    typedef enum {FALSE, TRUE} bool;
    
    
    QUEUE* CreateQueue(void);
    bool Enqueue(QUEUE* queue, void* dataptr);
    void* Dequeue(QUEUE* queue);
    void* QueueFront(QUEUE* queue);
    void* QueueRear (QUEUE* queue);
    bool EmptyQueue (QUEUE* queue);
    bool FullQueue (QUEUE* queue);
    int QueueCount (QUEUE* queue);
    void DestroyQueue(QUEUE* queue);
    that is my header file.

    Also, in main, everything is going okay and then you igmore your malloc'ed int and just pass in 5 directly. What's with that?
    typo, I fixed it
    Last edited by slee8251; 10-08-2012 at 12:35 PM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by iMalc View Post
    On line 13 you are not initialising queue->count, you are instead creating and not using a separate variable then goes out of scope shortly after.
    Also, in main, everything is going okay and then you igmore your malloc'ed int and just pass in 5 directly. What's with that?
    Did the problem go away after fixing "queue->count" initialization?
    Note, if you did not fix it, why not?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    23
    Yes, the problem did go away after I fixed the initialization.
    Appreciate the second pair of eyes to look over my dumb mistakes.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (queue*)this)->queue::qput’ does not have class type
    By brack in forum C++ Programming
    Replies: 13
    Last Post: 11-11-2010, 03:41 PM
  2. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  3. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  4. queue
    By tiknsemaj in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2002, 04:43 AM
  5. Queue and Priority Queue
    By Pamela in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2001, 11:09 PM