Thread: Pushing a Queue onto a Stack?

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

    Pushing a Queue onto a Stack?

    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, "&#37;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?
    Last edited by MiroMage; 10-14-2008 at 09:26 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So your train is a queue of ints, representing the car numbers. That means your station needs to be a stack of trains (not ints), or at least pointers to trains, and then you would do s->store[s->top] = q.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Why store the train's car number in a queue and on a stack i.e. two different places when they can all be made to fit on a stack for easy storage and retrieval. The queue and stack are complementary data structures. The queue is a FIFO while a stack is a LIFO. As the last train must be the first one to leave you need to use a LIFO type data structure which is a stack. So forget about using a queue and focus on pushing and popping items items on the stack. Can you list the members of the struct train and struct station types.
    Last edited by itCbitC; 10-14-2008 at 11:42 AM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22
    ^^ I figured I would need to make it so that I have a stack of queues, but I'm not sure how to put that into code. It would have to be something in the function when I create the stack, right? I'm not sure how to go about fixing that part.

    ^ I don't think I see what you are saying with being able to not use a queue, but I can post the two structs.

    Code:
    struct station
    {
        int* store;
        int top;
        int size;
    };
    
    struct train
    {
        int* store;
        int front;
        int back;
        int size;
    };

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MiroMage View Post

    Code:
    struct station
    {
        struct train * store;
        int top;
        int size;
    };
    
    struct train
    {
        int* store;
        int front;
        int back;
        int size;
    };

    Your station needs to store trains, not ints. (And of course you would need to define struct train first.)

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22
    Ah, alright. Thanks a lot! And the way it is stored on the stack will only store it once, right? Because when I put in a print function, it printed the second queue twice, the third queue three times, and so on. That might be a problem with my print function, but I just wanted to make sure that I am not really stacking the same queue multiple times.
    Last edited by MiroMage; 10-14-2008 at 09:41 PM.

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. Pushing a Queue Onto Stack
    By programming1985 in forum C Programming
    Replies: 4
    Last Post: 10-22-2008, 07:44 PM
  4. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  5. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM