• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Based Queue  RSS feed

 
Nick Kilo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jGRASP version 2.0.3.

I've been instructed to build an array based queue which loops the array storage back on itself.

I have two issues I'm hoping someone could provide guidance on:

1.) I had originally initialized 'front' to '0', but deque() was giving me an out of bounds error when 'front' would loop back to index '0', as I had 'return queue[front - 1];', and there is not index -1, as you know. So I initialized 'front' to '- 1' and solved my deque() out of bounds error, but it looks sloppy to me and I'm sure I'm simply missing the appropriate execution.

2.) I was hoping someone could explain how I might modify the deque() method to reset to the deque'd position back to '0000'. I find it confusing when it prints out and the old characters are present when I have deque'd many positions.

Please let me know if you have any questions.

Thanks





Output at CharQueue size [8]:
true
false
0
[ 0000 , 0000 , 0000 , 0000 , 0000 , 0000 , 0000 , 0000 ]
a
b
b
5
[a, b, c, d, e, f, g, 0000 ]
c
d
e
f
g
[a, b, c, d, e, f, g, 0000 ]
true
false
0

Output at CharQueue size [6]:
true
false
0
[ 0000 , 0000 , 0000 , 0000 , 0000 , 0000 ]
a
b
b
5
[ g, b, c, d, e, f ]
c
d
e
f
g
[ g, b, c, d, e, f ]
true
false
0
    
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven't gone through all your logic but this seems to be in need of a modulus operator
return queue[front + 1];
 
Nick Kilo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.

I have a modulous operator in the enque() and deque() methods, for looping the storage:





If I 'return queue[front + 1];' that would give me the second item in the queue, as the code sits currently.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at the one in peek().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!