queue problem

This is a discussion on queue problem within the C Programming forums, part of the General Programming Boards category; Here is improved version of my queue problem. To make it data nutural I'm passing the pointer to double that ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    4

    Question queue problem

    Here is improved version of my queue problem. To make it data nutural I'm passing the pointer to double that supposed to be converted to pointer to void impicitely.(See QueAdd function in the code) It still does not work. I can not get any values into the array, or I can not print them out. I'm not sure.
    Thanks for your time,
    Pete

    PS(I programming in C/C++);

    Here is the code:
    //Peter Yelagin
    //Queue4.c

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


    typedef struct queuetemplate
    {
    void** array;
    int size;
    int first;
    int last;
    }QUE;

    QUE* QueConstruct(int Size);
    void QueAdd(QUE *Q, void* Value);
    void QueRemove(QUE *Q, void* Destination);
    void QueDestruct(QUE *Q);
    int QueIsFull(QUE *Q);
    int QueIsEmpty(QUE *Q);

    int main(void)
    {
    double Destination;
    double Value;
    double* pVal;
    char command[60];
    int size=5;
    QUE* pQue;

    pQue=QueConstruct(size);

    for(;
    {
    fgets(command, sizeof(command), stdin);
    if(feof(stdin)) break;

    if(command[0]=='a')
    {
    if(QueIsFull(pQue)) printf("Queue is full.\n");
    else
    {
    pVal=(double*)malloc(sizeof(double));
    Value=atof(command+1);
    pVal=&Value;
    QueAdd(pQue, pVal);
    }

    }

    else if(command[0]=='r')
    {
    printf("%d\n", pQue->last);
    if(QueIsEmpty(pQue)) printf("Queue is empty.\n");
    else QueRemove(pQue, &Destination);
    }
    else continue;
    }

    for(int i=0; i<size; i++)
    {
    printf("%fd\n", *((double*)(pQue->array+i)));
    }

    QueDestruct(pQue);

    return 0;
    }


    QUE* QueConstruct(int Size)
    {
    QUE* que=(QUE*)malloc(sizeof(QUE));
    if(que==NULL)
    {
    perror("Oueue");
    exit(1);
    }
    que->array=(void**)malloc(sizeof(void*)*Size);
    if(que->array==NULL)
    {
    perror("Queue");
    exit(1);
    }
    que->size=Size;
    que->first=-1;
    que->last=-1;

    return que;
    }

    void QueAdd(QUE *Q, void* Value)
    {
    if(Q->last+1>Q->size-1) Q->last=-1;
    if(Q->last+1==Q->last) free(Value);
    Q->last++;
    Q->array[Q->last]=Value;//I think problem is here
    printf("%d\n", Q->last);//test output
    printf("%fd\n", *((double*)Value));//test output
    printf("%fd\n", *(double*)(Q->array)); //test output
    return;
    }

    void QueRemove(QUE *Q, void* Destination)
    {
    int next=Q->first;
    if(next+1>Q->size-1) next=-1;
    next++;
    Destination=Q->array[next];
    Q->first=next;
    return;
    }

    void QueDestruct(QUE *Q)
    {
    free(Q->array);
    free(Q);
    return;
    }

    int QueIsFull(QUE* Q)
    {
    //int next=Q->last;
    if(Q->last+1==Q->first) return 1;
    else return 0;
    }

    int QueIsEmpty(QUE* Q)
    {
    printf("%d\n", Q->last); //test out
    if(Q->last<0) return 1;

    //else if(Q->first==Q->last) return 1;
    else return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    I highly recommend rethinking your design. Take out a notepad and write down what you want the code to do in plain english so that you can get things straight before having to deal with syntax difficulties as well.
    Code:
    MAIN:
    
    Begin
      While not done
        Read a command from input
        If the command is add
          Read a value to add
          Add the value to the queue
        If the command is remove
          Remove an item from the queue
        If the command is unknown
          Notify the user
          Handle the error
      Loop
    End
    Follow a similar method with each function and then run through them all and refine them to the point you think they're perfect. Then try writing the code. The goal is to get it right the first time. If you make sure that your logic is right from the point you start writing code, you will have a lot less difficulty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem about stack and queue in C
    By neyul in forum C Programming
    Replies: 4
    Last Post: 04-07-2009, 10:34 PM
  2. Heap management
    By Frost Drake in forum C Programming
    Replies: 12
    Last Post: 10-15-2006, 10:06 PM
  3. Queues
    By ramayana in forum C Programming
    Replies: 22
    Last Post: 01-01-2006, 01:08 AM
  4. queue problem using c
    By doom83 in forum C Programming
    Replies: 3
    Last Post: 10-16-2005, 07:03 AM
  5. queue question
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2001, 05:06 PM

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