![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2008
Posts: 18
Rep Power: 0
![]() |
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++)
{
q(i) = CreateQueue(size 2);
}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 |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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. |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Feb 2008
Posts: 18
Rep Power: 0
![]() |
Re: How to name queues using a "for" loop??
Quote:
So do you mean I should do smthg as: #include <stdio.h>
#include <queue.h>
int main
{
Queue q[5];//where Queue is the abstract datatype that corresponds to queues
int i;
for(i=0;i<5;i++)
q[i]= CreateQueue(2); //queue size = 2
....
return 0;
} |
|
|
|
|
|
|
#4 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5
![]() |
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?
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#5 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Re: How to name queues using a "for" loop??
It's been a tough week for Ooble on PFO.
![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2008
Posts: 18
Rep Power: 0
![]() |
Re: How to name queues using a "for" loop??
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....
![]() |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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. |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2008
Posts: 18
Rep Power: 0
![]() |
Re: How to name queues using a "for" loop??
Thanks for the info guys!!
|
|
|
|
|
|
#9 |
|
Programmer
Join Date: Feb 2008
Location: India
Posts: 58
Rep Power: 1
![]() |
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!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Correct name for a variable length loop? | funkey_monkey | Other Programming Languages | 6 | Jun 2nd, 2007 1:50 AM |
| server-infinite loop | programmingnoob | C | 19 | Oct 1st, 2006 11:52 PM |
| Value of index incorrect after loop | aznluvsmc | C | 13 | Nov 6th, 2005 10:47 PM |
| WinSock accept() hangs program in Do loop... | layer | C++ | 5 | Apr 29th, 2005 12:28 PM |
| Timing loop problems | badbasser98 | C++ | 11 | Mar 10th, 2005 9:30 PM |