Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-17-2005, 10:23 AM   #1
Member (4 bit)
 
Join Date: Sep 2004
Posts: 13
Java Queue

For a lab, our class had to write a queue in java. Mine works fine, but my professor doesn't like how it "resets" when it is empty. He wants me to do it over, but I have no idea how else to write it. Anyone have a solution that doesnt set front and rear to 0 when it is empty?
Attached Files
File Type: zip Queue.zip (4.5 KB, 128 views)
aKobrakai is offline   Reply With Quote
Old 03-17-2005, 10:55 AM   #2
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
Were you supposed to use an array? Using a static type doesn't really make since to me when trying to model something dynamic like a queue.

Maybe he wanted you to use a linked list or a vector instead. This way you wouldn't have to use variables to keep track of locations... the structures would do that for you. Resetting would be as simple as something like x = new [Structure].
faulkner132 is offline   Reply With Quote
Old 03-17-2005, 11:49 AM   #3
Member (4 bit)
 
Join Date: Sep 2004
Posts: 13
We were forced to use an array of int. He wanted use to keep track of the top and bottom like I did. The only thing he really told us was that we shouldn't use one position in the array (I dont use the first position, but the way he showed us was a spot in the middle), and we should treat the array like a circle. He could be completely wrong, he is the worst professor I have had, and I highly doubt he has a working program the way he wants us to do it.
aKobrakai is offline   Reply With Quote
Old 03-17-2005, 12:12 PM   #4
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
If he wants you to think about it like a circle, then the best analogy I can come up with is a revolver. The space remains the same, yet the "start" of the structure changes everytime a bullet is fired. The first would change each time, but the last would only change when you load another bullet. This way you wouldn't have to reset, because when first = last it is already in the reset position.

I think this is a really poor way to teach a first in - first out concept. I prefer the simple lunch line example, but oh well. Hope I was of some help.
faulkner132 is offline   Reply With Quote
Old 03-18-2005, 08:16 AM   #5
Member (4 bit)
 
Join Date: Sep 2004
Posts: 13
What I don't understand is how to deal with the fact that first=last when the queue is empty, and when it is full.
aKobrakai is offline   Reply With Quote
Old 03-18-2005, 09:04 AM   #6
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
Good point, if there is no data in first, it is empty.

Think of the revolver again.
When it is fully loaded (6 bullets): first=1 and last=1
Fire: first=2 last=1
Fire: first=3 last=1
Fire: first=4 last=1
Fire: first=5 last=1
Fire: first=6 last=1
Fire: first=1 last=1 -- Empty (Condition is (first = last AND first has no data)

If you add data at any point during the dequeue process then simply increment last.
faulkner132 is offline   Reply With Quote
Old 03-20-2005, 11:28 AM   #7
Member (4 bit)
 
Join Date: Sep 2004
Posts: 13
Thanks for the response, but how do you check to see if there is no data on an array of int? I could be wrong, but I don't believe you can have null on an array of int.
aKobrakai is offline   Reply With Quote
Old 03-21-2005, 07:50 AM   #8
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
He should have given you a value (0, -1, etc.) to indicated there is no value. This way when you go to remove data from the queue you can reset the value. If he didn't then I have no idea what you can do short of creating a boolean which flags whether or not there is data.
faulkner132 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:19 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2