dequeueing issues

This is a discussion on dequeueing issues within the C Programming forums, part of the General Programming Boards category; The last part of my program is supposed to be able to remove one item from a queue at the ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22

    dequeueing issues

    The last part of my program is supposed to be able to remove one item from a queue at the top of a stack, but stop if there is only one item left in the queue. I can't figure out how to make it stop exactly. And also, after I remove the item and I go to print it out, the item I removed still prints as part of the queue (I know there is another post on this problem, but the fix would not work for my code). Here are my functions:

    First the dequeue function:
    Code:
    void remove_car(struct station* s, struct train* q)
    {   
        if(s->top == 0)
        {
            printf("Car not removed because there are no trains!\n");
            return;
        }
    
        s->top --;
        q = s->store[s->top];
        
        if(q->front + 1 == q->back)
        {
            printf("Car not removed because there is only one car on the train!\n");
            return;
        }
        printf("Car %d removed!\n", q->store[q->front]);
        q->front = (q->front + 1) % q->size;
    
        q = s->store[s->top];
        s->top ++;
        
        return;
    }
    And my printing function:
    Code:
    void train_departs(struct station* s, struct train* q)
    {
        int i;
        
        if(s->top == 0)
        {
            printf("Station empty, no train departed!\n");
            return;
        }
        
        s->top--;
        q = s->store[s->top];
        
        printf("Train Departed: ");
        
        for(i = q->front; i < q->size; i++)
        {
            printf("%d ", q->store[i]);
            q->front = (q->front + 1) % q->size;
        }
        printf("\n");
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your remove_car removes the train from the stack too. Somehow it seems unlikely that that's what you want. And I see no reason for passing in both a station and a train to remove_car -- either you're removing one car from the train given, in which case who cares about the station; or you're removing one car from the top of the stack of the station, in which case who cares about the train.

    Also, your remove_car function doesn't change q->size.
    Last edited by tabstop; 10-18-2008 at 10:20 PM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22
    Even if I cut it down, I still can't get it to work right. I made it a little shorter here:

    Code:
    int remove_car(struct train* q)
    {
        int val = q->store[q->front];
        
        if(q->front + 1 == q->back)
        {
            printf("Car not removed because there is only one car on the train!\n");
            return;
        }
        q->front = (q->front + 1) % q->size;
        
        return val;
    }
    And I have it returning the value it supposedly deleted. However, it's still not removing it from the list when I print it out.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My edit was slightly wrong: q->size isn't supposed to change -- but your print function had better stop at q->back, unless you want all the extra deleted stuff to still print.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22
    hm.. alright. Thanks. I'll have to play with it because when I changed it to stop at q->back, it didn't print anything. Just a new line.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22
    I changed my loop in my print function to this:

    Code:
    for(i = q->front; i < q->back; i++)
        {
            printf("%d ", q->store[i]);
            q->front = (q->front + 1) % q->size;
        }
        printf("\n");
    But now my function does no print anything at all. Does anyone know what might be causing this problem?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Print out q->front and q->back before you start to see what they are. Are you using wrap-around? If so, you'll need to check !=, not <. Also, you are dequeueing as you print, so at the end of this print function you'd have an empty queue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solution to culling issues?
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 03-14-2006, 06:59 PM
  2. Major game issues
    By VOX in forum Game Programming
    Replies: 0
    Last Post: 01-18-2005, 08:40 AM
  3. Memory Issues
    By Padawan in forum C Programming
    Replies: 12
    Last Post: 04-03-2004, 03:10 AM
  4. gphoto2 issues
    By axon in forum Tech Board
    Replies: 3
    Last Post: 03-21-2004, 08:33 PM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM

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