• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Having trouble to understand the full code of Queue using Array

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guy's,

Sorry for asking this (probably stupid) question, and since i am Dutch, sorry if my English is off. But since i don't have a teacher and i am learning myself, i would like to have a better explanation for this code. Or perhaps some links to websites or video's which explain this more easy. I've just learned about Array's, the usage of constructors and methods. I am using the book Java, A beginners guide version sixth edition. First off the code, i hope it's ok this way, otherwise, so sorry:



Ok, here my questions:

1. Why has the size of the array has to be 1 bigger then the requested queue, like written in the constructor: q = new char(size+1) ? The book said it's because of the algorithm that the array has to be one size bigger then the size of the requested queue. I don't get this, how does it work?
2. I don't understand how this code part works: if(putloc==q.length-1) or if(getloc == putloc), it's to chec if a queue is full or empty, but how does this works exactly?
3. In the char get() method, why if the queue is empty it does this: return (char) 0; ?
4. In example, if the smallQ has the size of 5 (4+1), why can it only store the letters Z, X, Y, w and not V? If the array has five spaces, you can store 5 letters right?

Sorry for al the questions, i googled, watched on youtube, read in the book again, but the code kept my head spinning for almost two weeks now. If someone can explain this more easy, i would appreciate that. I prefer examples with a table of the array or something like that so i can visualize it as well, if someone knows a great video, website link or own explanation for this, it would be so helpful. Thanks in advance for your time.

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default value of a char in Java is \u0000

So, for example, if you want to store 4 characters in the queue, you create a queue with 5
spaces

array = {\u0000,\u0000,\u0000,\u0000,\u0000}

At this point, putloc and getloc are both 0

You enqueue 'Z'

putloc is increased by 1

The array becomes

array = {\u0000,'Z',\u0000,\u0000,\u0000}

You enqueue 'Y'

putloc is increased by 1

array = {\u0000,'Z','Y',\u0000,\u0000}

You enqueue 'X'

putloc is increased by 1

array = {\u0000,'Z','Y','X',\u0000}

You enqueue 'W'

putloc is increased by 1

array = {\u0000,'Z','Y','X','W'}

Now the value of putloc is 4, which is the length of the array minus 1, which means there is
no more space in the queue

Now suppose you dequeue elements.

How do you know when you have dequeued all the elements?

At this point putloc is 4, and getloc is 0.

You dequeue an element.

getloc becomes 1. You dequeue 'Z'

You dequeue another element.

getloc becomes 2. You dequeue 'Y'

You dequeue another element.

getloc becomes 3. You dequeue 'X'

You dequeue another element.

getloc become 4. You dequeue 'W'

Now the value of getloc is the same as the value of putloc

This is an indication that the queue is empty.

So the next time you try to dequeue, what is returned is (char) 0 from the get method. The reason
for doing this is that the get method returns a char so you need to return something that is a
char.

So to answer your questions

(1) You create an array with 1 space larger than you need because of the way the put method
and get method work. That is, before you enqueue an element, you increment putloc to 1

(2) When the value of putloc is equal to the length of the array - 1, you know the queue is full. When
getloc is equal to putloc, you know you have dequeued everything

(3) Again, since the method returns a char, you can't return a 0 directly. You could repace return (char) 0 with return ' '

(4) When the size of the array is 5, you can store 4 characters

Does this help?
 
Simon Penders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Keith Lynn wrote:The default value of a char in Java is \u0000

So, for example, if you want to store 4 characters in the queue, you create a queue with 5
spaces

array = {\u0000,\u0000,\u0000,\u0000,\u0000}

At this point, putloc and getloc are both 0

You enqueue 'Z'

putloc is increased by 1

The array becomes

array = {\u0000,'Z',\u0000,\u0000,\u0000}

You enqueue 'Y'

putloc is increased by 1

array = {\u0000,'Z','Y',\u0000,\u0000}

You enqueue 'X'

putloc is increased by 1

array = {\u0000,'Z','Y','X',\u0000}

You enqueue 'W'

putloc is increased by 1

array = {\u0000,'Z','Y','X','W'}

Now the value of putloc is 4, which is the length of the array minus 1, which means there is
no more space in the queue

Now suppose you dequeue elements.

How do you know when you have dequeued all the elements?

At this point putloc is 4, and getloc is 0.

You dequeue an element.

getloc becomes 1. You dequeue 'Z'

You dequeue another element.

getloc becomes 2. You dequeue 'Y'

You dequeue another element.

getloc becomes 3. You dequeue 'X'

You dequeue another element.

getloc become 4. You dequeue 'W'

Now the value of getloc is the same as the value of putloc

This is an indication that the queue is empty.

So the next time you try to dequeue, what is returned is (char) 0 from the get method. The reason
for doing this is that the get method returns a char so you need to return something that is a
char.

So to answer your questions

(1) You create an array with 1 space larger than you need because of the way the put method
and get method work. That is, before you enqueue an element, you increment putloc to 1

(2) When the value of putloc is equal to the length of the array - 1, you know the queue is full. When
getloc is equal to putloc, you know you have dequeued everything

(3) Again, since the method returns a char, you can't return a 0 directly. You could repace return (char) 0 with return ' '

(4) When the size of the array is 5, you can store 4 characters

Does this help?



First of all, thanks for your answer. I'm sorry to say it, but it still is all confusing to me. So, i want to make a queue in which i want to store 4 letters. Char has an initial value of null i understand ( i don't get why this is mentioned) , but why can't i use the index [0] of the array in this case? So in your example: array = {\u0000,\u0000,\u0000,\u0000,\u0000} and then store it like this: array = {Z,Y,X,W,V} ? Why has putloc to be incremented first?

Also the book says you enqueue at the rear of the queue, and dequeue from the front. But the back of the queue supposed to be index [4] or [5] right? In many examples the enqueue at index [0] first, but this is the front?! I guess i am confusing indexes, values and other stuff and it became a mess in my head. So sorry to ask for more explanation, because i really aprreciate the effort you've put into your answer, feeling so stupid right now.. Hope you can simplify it further..
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you add to and remove from the queue at different ends.

I think part of your confusion is thinking of the whole array as the queue.

This is not the case.

Only a portion of the array is the queue.

When you increment putloc, that means you are using more of the array.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> why can't i use the index [0] of the array in this case?
>Why has putloc to be incremented first?

There is no absolute requirement for it. It is just the way this particular implementation works.

You could write it so that you enqueue into the putloc and THEN increment the putloc variable.
You would have to make the corresponding change in the dequeue method.
I think the rest would remain pretty much the same. (Try it and see. It should still pass all the tests of the QDemo class)

I actually agree with you. That first index is "wasted space". And it doesn't have to be. It is possibly a carry-over from legacy code examples and c++ days? Who knows?
If you continue in the industry, you will often find code like that that has been written by someone in a 'non-optimal' manner.
The important question though : Does it work?

>Also the book says you enqueue at the rear of the queue, and dequeue from the front. But the back of the queue supposed to be index [4] or [5] right? In many examples the enqueue at index [0] first, but this is the front?!
The array is just storage space. It isn't the actual queue.
The "front" and "back" of the queue are tracked in the variables getloc and putloc.
The queue is the data stored in the array based on the in the indexes between getloc and putloc.

The concept of data structures like this, is that the underlying implementation of the queue doesn't matter to the end user.
As long as it looks like a queue, and behaves like a queue, the fact that it uses an array to implement things is irrelevant to them.
Whether it starts from index 0 and counts up, or at index 'max' and counts down, or even 'wraps around' so that when you get to the end you start again at the beginning (as long as it isn't still being used there)


 
Marshal
Posts: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:. . .
I actually agree with you. That first index is "wasted space". And it doesn't have to be. It is possibly a carry-over from legacy code examples
. . .

And nobody is going to bother to change it in order to save 8 bytes of memory

Now that memory is cheap it is probably not worth changing such things for a tiny gain. But when you are writing new code, you might as well write it according to current best practice.
 
Simon Penders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the answers, i am starting to get it a little more. By the way, why is there a return statement in a void method anyway? A void method does not return a value anyway, right? Also, can't you just use a void method for the get method, so you don't have to use return (char) 0; ?

Also, why in this code they did this: Queue bigQ = new Queue(100); ? Because to store the alphabet it's enough to have a supporting array with the size of 27 right?

 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why is there a return statement in a void method anyway? A void method does not return a value anyway, right?


It's a way of exiting the method early.

Also, can't you just use a void method for the get method, so you don't have to use return (char) 0; ?


The get() method needs to return something since it's declared char get(), so even when it exits early, it returns a character (not the best design, BTW).
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:I actually agree with you. That first index is "wasted space". And it doesn't have to be. It is possibly a carry-over from legacy code examples and c++ days? Who knows?



It actually looks like it might have been a left-over (or in preparation) of a circular buffer. With circular buffers it's not easy to determine the difference between an empty and a full buffer. A common solution is to make the buffer size one bigger than it needs to be.
 
Simon Penders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for the answers. Only one question remains, why they have made a array of size 100 for storing the alphabet? You can simply use an array of 27 in this case right?

So, to sum it all up: if i understand correctly, two array objects are created in this code, thanks to the constructor those are full objects. One array object is made with the name smallQ and one bigQ. Both have the variables putloc and getloc. In example Z is inserted via the put method, but putloc is incremented first. So smallQ array has the letter Z on the index [1], Y on index [2] and so on. If putloc and getloc are both 0 then the Queue is empty, if putloc is the same as the array size-1 it is full. Correct thinking this way? If anyone knows some great video's or other tutorials about this Queue, please let me know. So i can get deeper into the material, i really like Java, but sometimes it can be hard suddenly..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic