Like Tree1Likes

queues FIFO implementation

This is a discussion on queues FIFO implementation within the C Programming forums, part of the General Programming Boards category; So I have been working on this program from my instructors notes, google and lots of brain power. Basically I ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    210

    queues FIFO implementation

    So I have been working on this program from my instructors notes, google and lots of brain power. Basically I just have to implement inserting, deleting and printing a queue using the array way. I am now at the point where I need you guys to tell me what isn't correct in my code. Right now I am only inserting and printing a queue and later (hopefully) I'll do the delete part. I'll appreciate your help.
    Q.H (header file)
    Code:
    struct Queue_struct;
    typedef struct Queue_struct Q_type;
    
    
    /* Allocate a new Queue and initialize it to be empty. */
    Q_type *Q_new();
    
    
    /* Deallocate a Queue and free the memory */
    void Q_free(Q_type *);
    
    
    /* 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);
    
    
    /* remove an item from the front of the Queue */
    int Q_delete(Q_type *, int *errorflag);
    main.c
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "Q.h"    /* Queue header file */
    
    
    
    
    void showcommands()
    {
      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");
    }
    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 'n': free(myQ);
              myQ = Q_new();
              break;
            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);
        }
    
    
      Q_free (myQ);
      return 0;
    }
    Q.c (THE REAL STUFF)

    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
    
    
    
    
    typedef struct Q_struct {
      int contents[Q_MAX_SIZE];
      int front;
      int count;
    } Q_struct;
    
    
    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 (front < 0)
        return;
      if ( count >= front)
        {
          for(i = front; i <= count; i++)
            {
              printf("\n i = %d", i);
              printf(" %d ", contents[i]);
            }
        }
      else
        {
          for(i = front; i < Q_MAX_SIZE_; i++)
            {
              printf("\n i = %d", i);
              printf(" %d", contents[i]);
            }
          for(i = 0; i <= count; i++)
            {
              printf("\n i = %d", i);
              printf(" %d ", contents[i]);
            }
        }
    }
    Last edited by kiwi101; 12-08-2012 at 02:58 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well you forgot to write why you are not happy with your code.

    Tip : The second scanf in main should be with a space before %, thus
    Code:
    scanf(" %d",...);
    Why?
    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

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    210
    Oh because my code isn't compiling and I don't see why not

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    210
    well in the Q_print function my compiler says that count, front, contents aren't defined?
    WHAT?
    clearly they were defined when I was using them in the add function?

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    front count contents of what?????

    In your add function you request a member of a struct, but in the print you don't
    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

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    210
    could you elaborate I don't think I understand. From my view I think I am requesting a struct for both. Could you show me where I am requesting a struct in my add

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In add function, you nicely request for count member of Q (where Q is a pointer to your struct)
    Code:
    Q->count
    Now in the print you say
    Code:
    count
    I won't get the joy of you to 'kill' the bug !

    In order to kill it, use this weapon
    Name:  250px-QWERTY_keyboard.jpg
Views: 90
Size:  12.4 KB
    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

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    210
    Thanks, now that they are not undefined anymore, a new error exists in Q_new, Q_add and Q_print. Its the same error it says:
    "dereferencing pointer to incomplete type"
    I'm pretty sure it has to do with all of the Q-> to something lines
    I have no clue what the error means though

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome Would you like to post your updated code?
    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

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    210
    Sure.
    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
    
    
    
    
    typedef struct Q_struct {
      int contents[Q_MAX_SIZE];
      int front;
      int count;
    } Q_struct;
    
    
    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]);
            }
        }
    }

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    In the code above you never define what Q_type is unless you meant to use Q_struct which you do typedef.

    Does "Q.h" contain the complete definition of Q_type? Because the compiler is telling you that you are trying to dereference a pointer to a type that it hasn't
    yet seen the full definition of (Q_type in this case).

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by nonpuz View Post
    In the code above you never define what Q_type is unless you meant to use Q_struct which you do typedef.

    Does "Q.h" contain the complete definition of Q_type? Because the compiler is telling you that you are trying to dereference a pointer to a type that it hasn't
    yet seen the full definition of (Q_type in this case).
    Spend some seconds to scroll up to the first post...
    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

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    210
    I have a question and this may sound stupid but when I wrote this:
    typedef struct Queue_struct Q_type;

    what does the Q_type mean?
    Because I thought that the typedef struct was going to Q_type also.

  14. #14
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    You created a typename. However you never defined what a struct Queue_Struct contains, so Q_type is an "incomplete type".

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by kiwi101 View Post
    I have a question and this may sound stupid but when I wrote this:
    typedef struct Queue_struct Q_type;

    what does the Q_type mean?
    Because I thought that the typedef struct was going to Q_type also.
    It means that Q_type is exactly the same with struct Queue_struct They are like sinonims.
    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

Page 1 of 4 1234 LastLast
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, 12:32 PM
  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, 07:58 PM
  4. BST implementation of priority queues
    By jcleong in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 10:09 AM
  5. FIFO help
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-15-2002, 12:57 PM

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