Originally Posted by
Martin_T
Am I on the correct track or miles off?
Your are close. Here's a few technical details to consider. This is your prototype:
Code:
int queue_init(MyQueue *q);
Notice "q" is a pointer.
Now, you have two choices. #1, if you want to declare and pass in a "MyQueue" like this:
Code:
MyQueue queue1;
queue_init(&queue1);
First, notice I used the "address of" operator (&) since queue1 was not declared a pointer. That means queue1 already has assigned local stack space in the calling function (presumably main), so you should not malloc anything in queue_init IF you want to go that route.
The other choice is to declare and submit a pointer:
Code:
MyQueue *queue1;
queue_init(queue1);
In this case, you DO need to malloc heap space for queue1. This is possibly better, depending on how the queue1 variable is going to be used.
There is, I think, a significant issue to consider.
3. Write a main() function that uses the functions from myqueue.h, and allows the user to:
* Create
new queues.
That means you will need an indeterminate number of variables in main() to represent the queues! You cannot hardcode them (queue1, queue2, etc), you in effect need a dynamic array.
Main() should contain the "user interface", ie, a simple menu of choices in the form of a switch/case, one of which is going to be "create new queue". Furthermore, you will also need a way to "select active queue" unless you intend to offer a choice of available queues everytime the user wants to add an item to a queue, etc (getting to sound like a lot of work, isn't it?) IMO, your prof made a bad call not including some kind of char "label" in the MyQueue struct. Without that, you will have to keep track of the user's "name" for each queue seperately, perhaps a parallel array of these:
Code:
typedef struct _listing {
MyQueue *que;
char label[32];
} QueueTag;
Anyway, here's a possibility for the dynamic array:
Code:
int i;
MyQueue **queues=NULL, **tmp=NULL;
for (i=0; i<6; i++) {
tmp = realloc(queues,(i+1)*sizeof(MyQueue*));
if (tmp) queues = tmp;
else return -1; /* handle error */
queues[i] = malloc(sizeof(MyQueue));
queues[i]->front = NULL;
queues[i]->back = NULL;
}
This creates an array of six queues. The last three lines would be taken care of in a call to init_queue (probably also a good place to maintain the global QueueTag list), this is just an illustration -- obviously, you do not want to arbitrarily create an array of six, but you do want to add one each time the user creates a new one, in more or less the same way.
Some things to note:
- **queues is a "pointer to pointer(s)"
- **tmp is just to allow us to check the return value of realloc. Really, you should also check the return value of the malloc() in a similar way (but "out of memory" errors are rare, or even impossible on eg, a normal linux system)
- **queues is allocated space for pointers -- sizeof(MyQueue*), but each element in queues is a pointer and needs further space to actually contain data, so it is malloced sizeof(MyQueue) BEFORE we set its internal elements "front" & "back" (even tho they are just set to NULL, NULL is still a value that requires a place to be stored).
Originally Posted by
Martin_T
I think with me not even being able to do this (last weeks work) I might see if I can drop this module.
If you are studying programming, I presume you will have to redo this in time. Also, if you are studying programming, IMO you better deal with this now, it involves a number of essential and fundamental concepts in C.
If you are not studying programming, you may want to consider that this looks like a labour intensive course and whether you can eat everything you put on your plate.