help with queues

This is a discussion on help with queues within the C Programming forums, part of the General Programming Boards category; Ive spent about 5 hours trying to put this together and I get nothing for my effort. Maybe Im just ...

  1. #1
    Unregistered
    Guest

    help with queues

    Ive spent about 5 hours trying to put this together and I get nothing for my effort. Maybe Im just a beginner who shouldnt be in this class. Can anyone help me on getting this queue project to work.And if you dont want to help do you know where i can learn more about queues . THanx.
    Here is the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include "queues.h"

    /*Que ADT type definition*/

    typedef struct node
    {
    void *dataPtr;
    struct node *next;
    } QUEUE_NODE;

    typedef struct
    {
    QUEUE_NODE *front;
    QUEUE_NODE *rear;
    int count;
    } QUEUE;

    /*prototype declarations*/
    QUEUE *createQueue (void);
    QUEUE *destroyQueue (QUEUE *queue);

    int dequeue (QUEUE *queue, void **itemPtr);
    int enqueue (QUEUE *queue, void *itemPtr);
    int queueFront (QUEUE *queue, void **itemPtr);
    int queueRear (QUEUE *queue, void **itemPtr);
    int queueCount (QUEUE *queue);

    int emptyQueue (QUEUE *queue);
    int fullQueue (QUEUE *queue);

    /*End of Queue ADT defintions*/

    int main (void)
    {
    char letter;

    printf("Choose a function to perform\n"
    "A. Enqueue data into queue\n"
    "B. Dequeu and print data\n"
    "C. Print data at the front\n"
    "D. Print data at the rear\n"
    "E. Print entire queue\n"
    "F. Print queue status: Empty\n"
    "G. Print queue status: Full\n"
    "H. Print number of element in queue\n"
    "I. Destroy queue and quit\n");
    scanf("%c",&letter);



    switch(letter)
    {
    case 'A'rintf("Enqueue data into queue\n");break;
    case 'B'rintf("Dequeu and print data\n");break;
    case 'C'rintf("Print data at the front\n");break;
    case 'D'rintf("Print data at the rear\n");break;
    case 'E'rintf("Print entire queue\n");break;
    case 'F'rintf("Print queue status: Empty\n");break;
    case 'G'rintf("Print queue status: Full\n");break;
    case 'H'rintf("Print number of element in queue\n");break;
    case 'I'rintf("Destroy queue and quit\n");break;
    defaultrintf("bad choice\n");
    }

    return 0;
    }
    /*createQueue*/
    QUEUE *creatQueue (void)
    {
    QUEUE *queue;

    queue = (QUEUE *) malloc (sizeof (QUEUE));
    if (queue)
    {
    queue->front = NULL;
    queue->rear = NULL;
    queue->count = 0;
    }
    return queue;
    }
    /*creatQueue*/

    /*enqueue*/
    int enqueue (QUEUE *queue, void *itemPtr)
    {
    QUEUE_NODE *newPtr;

    if (!(newPtr = (QUEUE_NODE *)malloc(sizeof(QUEUE_NODE))))
    return 0;
    newPtr->dataPtr = itemPtr;
    newPtr->next = NULL;

    if (queue->count ==0)
    queue->front = newPtr;
    else
    queue->rear->next = newPtr;

    (queue->count)++;
    queue->rear = newPtr;
    return 1;
    }
    /*enqueue*/

    /*dequeue*/
    int dequeue (QUEUE *queue, void **itemPtr)
    {
    QUEUE_NODE *deleteLoc;

    if (!queue->count)
    return 0;
    *itmePtr = queue->front->dataPtr;
    deleteLoc = queue->front;
    if (queue->count == 1)
    queue->rear = queue->front = NULL;
    else
    queue->front = queue->front->next;
    (queue->count)--;
    free (deleteLoc);

    return 1;
    }
    /*dequeue*/

    /*queueFront*/
    int queueFront (QUEUE *queue, void **itemPtr)
    {
    if(!queue->count)
    return 0;
    else
    {
    *itemPtr = queue->front->dataPtr;
    return 1;
    }
    }
    /*queueFront*/

    /*queueRear*/
    int queueRear (QUEUE *queue, void **itemPtr)
    {
    if(!queue->count)
    return 0;
    else
    {
    *itemPtr = queue->rear->dataPtr;
    return 1;
    }
    }
    /*queueRear*/

    /*emptyqueue*/
    int emptyQueue (QUEUE *queue)
    {
    return (queue->count == 0);
    }
    /*emptyqueue*/

    /*fullQueue*/
    int fullQueue (QUEUE *queue)
    {
    QUEUE_NODE *temp;

    if ( (temp = (QUEUE_NODE *)malloc (sizeof (QUEUE_NODE))) )
    {
    free(temp);
    return 0;
    }

    return 1;
    }
    /*fullQueue*/

    /*queueCount*/
    int queueCount ( QUEUE *queue)
    {
    return queue->count;
    }
    /*queueCount*/

    /*destroyQueue*/
    QUEUE *destroyQueue (QUEUE *queue)
    {
    QUEUE_NODE *deletePtr;

    if (queue)
    {
    while (queue->front != NULL)
    {
    free (queue->front->dataPtr);
    deletePtr = queue->front;
    queue->front = queue->front->next;
    free (deletePtr);
    }
    free (queue);
    }
    return NULL;
    }
    /*destroyQueue*/

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Place code tags around it to preserve the original indention and disable smilies, then I'll take a look.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    whats a code tag

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating two static queues?
    By difficult.name in forum C Programming
    Replies: 2
    Last Post: 10-19-2004, 12:19 AM
  2. stacks and queues
    By j0hnb in forum C Programming
    Replies: 4
    Last Post: 04-16-2003, 10:44 PM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 10:09 PM
  4. queues
    By jamesb in forum C Programming
    Replies: 1
    Last Post: 04-21-2002, 09:57 PM
  5. queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 06:19 PM

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