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");
}