Thread: queues FIFO implementation

  1. #16
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    typedef struct Queue_Struct {
      char * member1;
      int member2;
    } Q_type;
    This (above) is a complete type, the compiler knows what is inside a struct Queue_Struct.

    This (below) is an INcomplete type:
    Code:
    typedef struct Queue_Struct Q_type;

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    210
    Hmm...okay.
    So inorder to fix this error of dereferencing I should change my Q_type's to Q_struct?

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    210
    If I wanted to define (which I guess I have to) my struct Q_struct? What would I even put in it?

  4. #19
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Correct, the way you are accessing Q you clearly want it to be a struct Q_struct, which is typedef'd to Q_struct. So Instead change it to:
    Code:
    typedef struct Q_struct {
    ...
    } Q_type;
    It sounds however like you need to review structures and typedefs in the first place if you don't even understand why you are doing what you are doing. Did you even write this code or did you copy it from somewhere?

  5. #20
    Registered User
    Join Date
    May 2012
    Posts
    210
    Please.. only I know the brainpower and tension it took me to come up with this code.
    My instructor did provide us with this skeleton code but it didn't help me as much as it could have and since I took ages to come up with this code I really want to understand everything. Thank you
    So the number of errors while compiling has decreased dramatically.
    Last edited by kiwi101; 12-08-2012 at 04:57 PM.

  6. #21
    Registered User
    Join Date
    May 2012
    Posts
    210
    Okay so the compiler says:
    I am redefining Q_type which in the header file looks like this:
    Code:
    typedef struct Q_struct Q_type;
    but in the Q.c file looks like this:
    Code:
    typedef struct Q_struct {
      int contents[Q_MAX_SIZE];
      int front;
      int count; (THIS LINE)
    } Q_type;
    Last edited by kiwi101; 12-08-2012 at 05:01 PM.

  7. #22
    Registered User
    Join Date
    May 2012
    Posts
    210
    So, how exactly am I redefining it?

  8. #23
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    By typedefing twice. Simply remove 'typedef' from the second declaration. So you have
    Code:
    typedef struct Q_struct Q_type;
    ...
    struct Q_struct {
    ...
    };

  9. #24
    Registered User
    Join Date
    May 2012
    Posts
    210
    Thank you so much with this structure stuff I know I'm confused with it but some of the confusion has disappeared
    ITS COMPILED!!!
    okay first thing I got a warning for this line in my main saying "assignment makes pointer from integer without a cast"
    myQ = Qnew();

    and now that my program is running (THANK GOD!!)
    it's not printing anything or inserting

    Q.h
    Code:
    struct Q_struct;
    typedef struct Q_struct Q_type;
    
    
    
    
    /* Allocate a new Queue and initialize it to be empty. */
    Q_type *Q_new();
    
    
    
    
    
    
    
    
    
    
    /* Print the Queue in an attractive fashion */
    void Q_print (Q_type *);
    
    
    
    
    /* error codes for errorflag returned by Q_add and Q_delete */
    #define ERROR_FULL_QUEUE 0
    #define ERROR_EMPTY_QUEUE -1
    #define EVERYTHING_OK 1
    
    
    
    
    /* add an item at the end of the Queue */
    void Q_add(Q_type *, int, int *errorflag);
    Q.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "Q.h"
    
    
    /*********************************************************
    Implementation of the Queue ADT using a fixed size array.
    *********************************************************/
    
    
    /* Queue size must be an array of size 10 */
    #define Q_MAX_SIZE 10
    
    
    
    
    struct Q_struct {
      int contents[Q_MAX_SIZE];
      int front;
      int count;
    };
    
    
    Q_type *Qnew()
    
    
    {
      Q_type *Q;
    
    
      Q = (Q_type *)malloc(sizeof(Q_type));
    
    
      Q->front = 0;
      Q->count = 0;
    
    
      return Q;
    }
    
    
    void Q_add(Q_type *Q, int value, int *errorflag)
    {
      int newElementIndex;
    
    
      if (Q->count >= Q_MAX_SIZE)
        { *errorflag = ERROR_FULL_QUEUE;
          return;
      }
      *errorflag = EVERYTHING_OK;
      newElementIndex = (Q->front + Q->count) % Q_MAX_SIZE;
      Q->contents[newElementIndex] = value;
    
    
      Q->count++;
    }
    
    
    void Q_print (Q_type *Q)
    {
      int i;
    if (Q->front <= 0)
        return;
      if ( Q->count >= Q->front)
        {
          for(i = Q->front; i <= Q->count; i++)
            {
              printf("\n i = %d", i);
              printf(" %d ", Q->contents[i]);
            }
        }
      else
        {
          for(i = Q->front; i <Q_MAX_SIZE; i++)
            {
              printf("\n i = %d", i);
              printf(" %d", Q->contents[i]);
            }
          for(i = 0; i <= Q->count; i++)
            {
              printf("\n i = %d", i);
              printf(" %d ", Q->contents[i]);
            }
        }
    }
    main.c
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "Q.h"    /* Queue header file */
    
    
    
    
    
    
    
    
    void show_commands()
    {
      printf("Queue Processing Commands:\n");
      printf("==========================\n");
      printf("n : \n");
      printf("p : \n");
      printf("a : \n");
      printf("d : \n");
      printf("v : \n");
      printf("e : \n");
      printf("f : \n");
    }
    int main(void)
    {
      Q_type *myQ;
      myQ = Qnew();  /* create an empty Q data object */
    
    
    
    
      char command;   // store current command
      int errorflag;      // possible error code
      int value;          // value to be inserted or returned
      float avg;          // average of queue values
    
    
    
    
      show_commands();
      printf("Queue command? ");
      scanf("%c", &command);
    
    
    
    
      while (command != 'q')
     {
          switch (command)
            {
            case 'p': Q_print(myQ);
              break;
            case 'a': printf("Enter value to insert: ");
              scanf("%d", &value);
              Q_add(myQ, value, &errorflag);
              if (errorflag == ERROR_FULL_QUEUE)
                printf("Queue full -- insert failed.\n");
              break;
            default: printf("Command not recognized\n");
              show_commands();
            }
          printf("Queue command? ");
          scanf("%c",&command);
        }
      return 0;
    }
    So, is it because my code is wrong or because of that warning since I tend to ignore warnings

  10. #25
    Registered User
    Join Date
    May 2012
    Posts
    210
    When I insert the value it takes it as a command and says command not recognized whereas the value should go straight to Q_add function, why isn't it?

  11. #26
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    See post #2, where I made the mistake and written %d instead of %c, which was what I wanted to say.

    What input to you give?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #27
    Registered User
    Join Date
    May 2012
    Posts
    210
    Oh sorry yeah fixed that still same error
    here is my output
    Code:
    Queue Processing Commands:
    ==========================
    n :
    p :
    a :
    d :
    v :
    e :
    f :
    Queue command? a
    Enter value to insert: 12
    Queue command? Command not recognized
    Queue Processing Commands:
    ==========================
    n :
    p :
    a :
    d :
    v :
    e :
    f :

  13. #28
    Registered User
    Join Date
    May 2012
    Posts
    210
    see it's ignoring my inserted value and processing it as a command?

  14. #29
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Read one of the tutorials on input. This is a problem with using scanf to read keys from the user. The \n is being left in the buffer so your next
    call to scanf reads that and automatically moves on. This can be "fixed" by simply placing a space in front of your calls like this:
    Code:
    scanf(" %c", &c);
    Still please read up on how to properly retrieve input from the user one of the 'fgets' tutorials.

  15. #30
    Registered User
    Join Date
    May 2012
    Posts
    210
    I definitely know how to use fgets. I've been doing it on my other projects. I'll demonstrate it if you don't believe me
    So.. It's inserting but still not printing I think there is something wrong with my command loop. Cuz this is how my output goes
    Code:
    Queue Processing Commands:
    ==========================
    n :
    p :
    a :
    d :
    v :
    e :
    f :
    Queue command? a
    Enter value to insert: 12
    Queue command? p
    Queue command? p
    Queue command?
    after the first 'p' it should've printed it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FIFO files
    By Belka in forum C Programming
    Replies: 5
    Last Post: 10-08-2012, 11:32 AM
  2. FIFO problemss.. Please help? =)
    By c3jcarmy in forum C Programming
    Replies: 1
    Last Post: 11-25-2011, 05:04 PM
  3. Nonblocking FIFO
    By Teegtahn in forum C Programming
    Replies: 14
    Last Post: 05-02-2007, 06:58 PM
  4. BST implementation of priority queues
    By jcleong in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 09:09 AM
  5. FIFO help
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-15-2002, 11:57 AM