i just have a feeling it has to do with the way the queue is being stacked. lets say we have 2 queues in the stack. if i pop the queue on top, and then try to add to the queue left over, it starts a new queue. it should add it to the left over queue. if i pop once, there is still "something" in the stack and if i pop again it tells me it is empty now.
You didn't need my print function -- you already had print train; I changed to this
Everything prints beautifully.Code:void print_station(struct station* s) { int i; for(i=0; i < s->top; i++) { printf("Train %d: ", i); print_train(&(s->store[i])); /*printf("%s ", s->store[i]); */ } printf("\n"); }
Other notes: By now you should have realized that free and malloc are in <stdlib.h>. Your add_car and remove_car use q, that is used nowhere else. (In other words, not the trains in the station.)
Well, the question is: where do you want the car added? To the train on top of the stack? In that case, you'll need to send &(s->store[s->top-1]) rather than ... something else.
why s->top-1?
Because s->top doesn't have anything in it. It always points at where the next train will go, not at the train at the top of the stack.
ok i understand. wow its sooo good to see my queue in the stack for once. haha. well what do i do if i want to add a new car to the queue on the top of the stack?
ok but i dont understand how to send it to there and attach it to the train. also if i have a train depart, and add to the train under it, it just acts as if it is a new queue all together. should i pop the train and then add the car and push it back on?
Just typing seventeen words in a row does not always lead to a coherent sentence. Take a deep breath and try again.
Since the part I have highlighted is factually incorrect, I am unable to answer the question.also if i have a train depart, and add to the train under it, it just acts as if it is a new queue all together. should i pop the train and then add the car and push it back on?
*wheeew* ok
lets say there are 2 trains in the station. one of them leaves. now with that last train, i want to add a car to it. i am having two problems with this. first one is if i just add_car, it makes a new queue all together. when train_departs runs the print_station function, it prints an empty line. im sure i need a pointer somewhere. i though s->top-- would bring the stack down one and point at the next queue...
Okay. I got that to happen, sort of: add_car works fine (I'm assuming you managed at some point to trade off "q" for "&(s->store[s->top-1])"), but the second departure doesn't. Looking at the code again, you've forgotten one detail: the queue q (in main) is completely meaningless. When you're in, say, train_departs, the q that gets passed in has nothing in it. You shouldn't be trying to pop off cars from q, you need to be popping off cars from the train at the top of s. So again, I added
to the top of the train_departs thing and now that works.Code:q = &(s->store[s->top-1]);
you know what, right before i closed my laptop i noticed that and i tried it. it completely fixed my problem. i wasnt looking at the right queue. thanx for all your help tabstop!