Thread: printing a queue

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    71

    printing a queue

    i seem to be having a problem printing a queue. when i dequeue, i am returning the value that was dequeued, and letting the user know when the queue is empty, but when i go to print the queue, the values entered are still there. i think it has to do with my dequeue function. here's the code

    Code:
    //in main if choice = 6 then print the queue
            if(choice == 6)
            {
                print_queue(q);
            }
    
    //remove the front value
    int dequeue(struct queue* q)
    {
        int val = q->store[q->front];
        q->front = (q->front + 1) % q->size;
    
        return val;
    }
    
    //print queue function
    void print_queue(struct queue* q)
    {
        int j;
        
        for(j=0; j < q->back; j++)
            printf("%d ", q->store[j]);
            printf("\n");
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    for(j=q->front; j < q->back; j++)
    Edit: Also, I don't know if you're doing "rollaround" (where q->front might be 15 and q->back might be 2, so that the queue goes 15, 16, 17, 18, 19, 0, 1, 2), but if so, then you should check != rather than <.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    ok thanks that really helped!

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22
    How are you getting yours to print that way? When I changed my loop like that all it printed for me was a new line...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM