Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 22nd, 2008, 2:35 AM   #1
joeserhal
Newbie
 
Join Date: Feb 2008
Posts: 18
Rep Power: 0 joeserhal is on a distinguished road
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
joeserhal is offline   Reply With Quote
Old Mar 22nd, 2008, 2:51 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 22nd, 2008, 3:01 AM   #3
joeserhal
Newbie
 
Join Date: Feb 2008
Posts: 18
Rep Power: 0 joeserhal is on a distinguished road
Re: How to name queues using a "for" loop??

Quote:
Originally Posted by Ooble View Post
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.
Well, I'm using the queue.c and queue.h files, which contain the basic functions regarding queues (create, destroy, enqueue, dequeue, IsEmpty...)
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;
}
??
joeserhal is offline   Reply With Quote
Old Mar 22nd, 2008, 5:04 AM   #4
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,198
Rep Power: 5 lectricpharaoh will become famous soon enough
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
lectricpharaoh is offline   Reply With Quote
Old Mar 22nd, 2008, 10:57 AM   #5
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Re: How to name queues using a "for" loop??

Quote:
Originally Posted by lectricpharaoh View Post
Disregard Ooble's post
It's been a tough week for Ooble on PFO.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 22nd, 2008, 1:26 PM   #6
joeserhal
Newbie
 
Join Date: Feb 2008
Posts: 18
Rep Power: 0 joeserhal is on a distinguished road
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....
Attached Files
File Type: c queue.c (1.2 KB, 3 views)
File Type: h queue.h (327 Bytes, 3 views)
joeserhal is offline   Reply With Quote
Old Mar 23rd, 2008, 4:15 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 23rd, 2008, 10:26 AM   #8
joeserhal
Newbie
 
Join Date: Feb 2008
Posts: 18
Rep Power: 0 joeserhal is on a distinguished road
Re: How to name queues using a "for" loop??

Thanks for the info guys!!
joeserhal is offline   Reply With Quote
Old Mar 24th, 2008, 3:57 PM   #9
rhish.franks
Programmer
 
Join Date: Feb 2008
Location: India
Posts: 58
Rep Power: 1 rhish.franks is on a distinguished road
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!
rhish.franks is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC