class Queue {
int front, rear, max, size;
int items[];
public Queue (int s) {
front = rear = -1;
max=5;
size=0;
items = new int [max];
}
// should we still include the "front" in this section?
public boolean enqueue (int x) {
if (size==max)
return false;
else {
items [rear+1]=x;
size++;
rear++; //rear=size-1;
return true;
}

i have a question.. what does the "front" do? should it be included in the public boolean enqueue (int x) ..........??

>what does the "front" do?
A queue allows you to work with both "ends" of the data structure. You enqueue (add items) onto the rear and dequeue (remove items) from the front. This gives you the first-in-first-out behavior of a queue. So, front is used just like rear, except on the other end of the array.

>should it be included in the public boolean enqueue (int x) ..........??
Yes, it should be, but this implementation seems to be naive in that it doesn't consider that situation. An array-based queue should take into account the current front and rear of the queue so that you don't end up shrinking the maximum available size after a few dequeue operations[1][2].

[1] For extra credit, figure out how that would happen. Here's a hint: Enqueue 3 items to a queue that can hold 5, then dequeue those three items and try to enqueue 3 more.

[2] For super extra high calorie credit, figure out how to fix the problem.

umm how do i implement the "front" to the public boolean enqueue (int x) ??

thanks for replying :)

>umm how do i implement the "front" to the public boolean enqueue (int x) ??
If I told you, that would defeat the purpose of my exercises, wouldn't it?

waaa.. its ok... hehe.. im not really good at programming thats why... heheheh

>im not really good at programming thats why...
Then you're probably not quite ready for a circular queue implementation. In that case, enqueue doesn't need to use front at all.

but its a requirement for me hehehe.. ok ok.. thanks :) :)

>but its a requirement for me hehehe..
You're required to use front in the enqueue method? Maybe you should describe exactly what your assignment is so that I don't have to play 20 questions with you.

not really required but the fact that our prof is asking if it is possible, then maybe it is required to include the "front"...

It's possible, but not needed.

why is it not needed? hehehe

>why is it not needed? hehehe
You're just screwing with me, aren't you?

edit edit... the instructions says that we should be able to put the "front" in the public boolean enqueue (int x).. (what is it called btw? enqueue method?) to be able to see the importance of "front"..

>to be able to see the importance of "front"..
If enqueue places an item at the rear of the array, then front is only important in the inverse operation: dequeue. I fail to see how your teacher expects you to see the importance of something when you have no idea what that thing even does. Post the rest of the class so I have a better idea of what's going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.