The basic idea of this program is to keep track of different trains that come into a train station, and store each train's car number in a queue. The last train to enter must be the first train to leave, so I know I need to somehow store the queue holding the trains' information in a stack. However, I keep getting a strange output when I tried to push the queue directly onto the stack. I'm not sure if I am having an issue where I first created the stack, or I am just not using the push command properly.
Here is the code where I initialized the queue and stack:
Code:
struct station* new_station(int Initial_Size) //my stack
{
struct station* s;
s = malloc(sizeof(struct station));
s->store = calloc(Initial_Size, sizeof(int));
s->top = 0;
s->size = Initial_Size;
return s;
}
struct train* new_train(int initial_size) //my queue
{
struct train* q;
q = malloc(sizeof(struct train));
q->store = calloc(initial_size, sizeof(int));
q->front = 0;
q->back = 0;
q->size = initial_size;
return q;
}
And here is the function I came up with to store the information:
Code:
void add_train(struct station* s, struct train* q, int num_cars, FILE* finfo)
{
int i;
int j;
int car;
for(i = 0; i < num_cars; i++)
{
fscanf(finfo, "%d", &car);
q->store[q->back] = car;
q->back = (q->back + 1) % q->size;
//expand queue check
}
s->store[s->top] = q->store;
s->top++;
for(j = 0; j < s->top; j++)
{
printf("%d\n", s->store[j]);
}
return;
}
I'm not sure what I can do to fix this problem. Anyone have any ideas?