|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#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?
|
|
|
|
|
|
#2 |
|
Come in Ray...
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]. |
|
|
|
|
|
#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.
|
|
|
|
|
|
#4 |
|
Come in Ray...
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. |
|
|
|
|
|
#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.
|
|
|
|
|
|
#6 |
|
Come in Ray...
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. |
|
|
|
|
|
#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.
|
|
|
|
|
|
#8 |
|
Come in Ray...
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.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|