tags:

views:

47

answers:

1

Hi,

I am working on a simulation. For this simulation I need 20 nodes (static) and then each node has a queue associated with it. This queue could hold any number of values. What would be the best way to implement this in C? I was aiming at having each of the queues being a simple linked list, but how can I effectively create several queues, one for each node??

Thanks, Any help would be most appreciated

+2  A: 

Basically, you need to create one struct for the queue, which is a simple linked list:

typedef struct queue_t {
    int data; /* the date in the queue, can be anything, not only an int */
    struct queue_t* next; /* pointer to the next in the queue */
} queue_t;

And then another one is the list of 20 queues:

queue_t *list_of_queues[20];

That's the simplest way to go about it, IMHO.

Edit: Made the array of structs into an array of pointers

Nathan Fellman
would you have to define a pointer to the head of the list, for each queue then? would that be list_of_queues[1] * HeadList = NULL
stephen
@stephen: I would change the above example to `struct queue_t* list_of_queues[20];` so that `list_of_queues[t]` would hold the top-of-queue pointer for node `t`.
bta
@bta: I agree. I changed the example as you suggested.
Nathan Fellman