![]() |
How to name queues using a "for" loop??
Hi there,
I'm facing some trouble with creating and naming queues in C language. I need to create five queues using a "for" loop, and with each iteration of the "for" loop, a created queue will be given a name depending on the index of the iteration. It should look like something like this: :
for(i=0;i<5;i++)At the first iteration, I should get a queue called q0 or something, then q1.... I need this because, at a later stage in my program, i need to access,through a "for" loop, a particular queue depending on the index of the iteration. Does anybody know how i can do this??.... Thanks |
Re: How to name queues using a "for" loop??
Are you creating the queue entirely yourself or can you use one that's been built for you?
If you're allowed to use a built-in library, try the queue class of the standard library. As you'll need five, make an array of five of them.:
std::queue<queue type> q[5];That should be all you need to create them. Obviously, you'll need to replace "queue type" with whatever type of variable you're storing in it. You can then use the push and pop methods to store and remove information. In addition, front will return the frontmost item in the queue. |
Re: How to name queues using a "for" loop??
Quote:
So do you mean I should do smthg as: :
#include <stdio.h> |
Re: How to name queues using a "for" loop??
You've got the idea. Disregard Ooble's post, as it's for C++ rather than C (unless you posted this in the wrong forum, and meant to post in the C++ forum). I'm assuming this is for school, and your instructor has given you the queue ADT, or you got them from the textbook.
You will need to create an array of queues, and then initialize each element of the array. Now, depending how your queue type is implemented, you may need to allocate an array of pointers instead, but I doubt it. Can you post the contents of your queue.c and queue.h files? |
Re: How to name queues using a "for" loop??
Quote:
|
Re: How to name queues using a "for" loop??
2 Attachment(s)
Yup, I've attached them...To tell you the truth, I don't know how I didn't think about using an array for creating the qeues, I think I was so focused on naming the queues individually (q0, q1, q2....) that I forgot I could just easily store them in one array and then reference them using a particular index....:rolleyes:
|
Re: How to name queues using a "for" loop??
Goddamnit, should pay more attention to the forum title. Sorry about that. It seems to have helped a bit though, so not all is lost. :)
The same logic I used in my previous post applies. Enqueue will push ints (typedef'd to ElementType, but you don't really need to worry about that) to the queue specified, [icode]Dequeue will pull the frontmost element off the queue and return it. You also have two handy functions, IsEmpty and IsFull, which basically do what they say on the tin.It's important to note that you need to use the DestroyQueue function on each queue after you've finished to free up the memory properly. |
Re: How to name queues using a "for" loop??
Thanks for the info guys!!
|
Re: How to name queues using a "for" loop??
You can use two dimensional array for that purpose, for using queue, one index for each entry of particular queue will remain constant! i.e. either row or column will remain constant for particular queue!
|
| All times are GMT -5. The time now is 6:00 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC