Thread: Dequeue

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Dequeue

    I have a queue with items of both type int and char. When I dequeue these how can I check if the item is an int or a char? Should I dequeue them generically, using a pointer to the item and then check? How can I go about doing this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You queue these
    Code:
    struct queueElement {
      enum { IS_CHAR, IS_INT } type;
      union {
        char c;
        int i;
      } v;
    };
    Say
    var.type = IS_CHAR;
    var.v.c = 'a';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    so when i originally make my queue i enqueue the items using that code? then, when dequeuing, i check if var.type = IS_CHAR ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    thank you!

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    okay i need a little bit more help here...
    i don't really understand how to store the var.type as part of the queue...
    how do i create the queue so that the structure is part of each element? so that when dequeuing i am able to check the var.type component?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You have
    struct queueElement aQueue[10];

    And say
    int insertPoint = 0, removePoint = 0, numEntries = 0;

    An insert function would be
    aQueue[insertPoint++] = item; numEntries++;

    A remove function would be
    item = aQueue[removePoint++]; numEntries--;

    The rest is just error checking.

    If you want to use a linked list for the queue, that's your choice, but the fundamentals don't change.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Post your code, otherwise we're going to have to guess.
    You'd probably have to change the type of the items stored in the queue from 'int' to 'struct queueElement'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. enqueue and dequeue pointer
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2007, 03:21 PM
  3. Prefix Evaluation
    By jcramer in forum C Programming
    Replies: 3
    Last Post: 04-19-2004, 12:46 PM
  4. dequeue
    By jk81 in forum C Programming
    Replies: 2
    Last Post: 11-27-2002, 09:48 PM