Need HelP In JaVa
Hi ,,
i need some help in my home work ..
any help will be appreciated..
Assume that a queue of objects is implemented by an array of size N in a circular fashion
Two variables keep track of the front and rear: front and rear, as described in the following class definition.
public class ArrayQueue {
private Object Q[];
private int front,rear;
private int size;
public ArrayQueue(int capacity) {
Q = new Object[capacity];
// variables front and rear are to be initialized here, based // on the requirements below
}
// methods are to be added here based on the requirements below
}
Complete the constructor for ArrayQueue and write the methods enqueue, dequeue, size, front, isEmpty. (Provide a separate solution for each of the following cases.)
1. front is the index of the first element, rear is the index of the last element and the location following the last element is always kept empty.
2. front is the index of the first element, rear is the index of the last element and the location preceding the first element is always kept empty.
3. front is the index of the location preceding the first element, which is always kept empty, rear is the index of the location following the last element, which is always kept empty.
4. . front is the index of the location preceding the first element, which is always kept empty, rear is the index of the last element. (Draw your own picture.)
Thanks
i need some help in my home work ..
any help will be appreciated..
Assume that a queue of objects is implemented by an array of size N in a circular fashion
Two variables keep track of the front and rear: front and rear, as described in the following class definition.
public class ArrayQueue {
private Object Q[];
private int front,rear;
private int size;
public ArrayQueue(int capacity) {
Q = new Object[capacity];
// variables front and rear are to be initialized here, based // on the requirements below
}
// methods are to be added here based on the requirements below
}
Complete the constructor for ArrayQueue and write the methods enqueue, dequeue, size, front, isEmpty. (Provide a separate solution for each of the following cases.)
1. front is the index of the first element, rear is the index of the last element and the location following the last element is always kept empty.
2. front is the index of the first element, rear is the index of the last element and the location preceding the first element is always kept empty.
3. front is the index of the location preceding the first element, which is always kept empty, rear is the index of the location following the last element, which is always kept empty.
4. . front is the index of the location preceding the first element, which is always kept empty, rear is the index of the last element. (Draw your own picture.)
Thanks
Replies
-
shalini_goel14Hi hio,
Please check the following piece of code showing how to write enqueue(), dequeue(), isEmpty() and size() methods in your class. I just made a rough class, based on your mentioned cases- you have to modify enqueue() and dequeue() methods accordingly. One more thing what you want to do in front() method ? I am not clear so I have not implemented that one.
/* * ArrayQueue.java * * Created on June 10, 2009, 9:28 AM */ package myjava; /** * * @author shalinig */ public class ArrayQueue{ public Object Q[] = null; private int front,rear; private int size; /** Creates a new instance of ArrayQueue*/ public ArrayQueue(int capacity) { Q = new Object[capacity]; front = 0; rear = capacity; size = 0; } public boolean enqueue(Object x) throws ArrayIndexOutOfBoundsException{ if((front + 1 ==rear ) || ((front +1 == size) && rear ==0)){ return false;//queue is full } else{ Q[front++] = x; size++; } return true; } public Object dequeue() throws ArrayIndexOutOfBoundsException{ if (rear == front) { // queue is empty return null; } else { // return object size--; Object obj = Q[rear]; Q[rear] = null; rear++; return obj; } } public int getSize(){ return this.getSize(); } public boolean isEmpty(){ return (size==0?true:false); } }
Let me know if any issues. ๐
Thanks ! -
hioi really appreciate your help ,,
but what have you done is for the first one only,,
can you help me with the other ,,
thanks,, -
shalini_goel14
Ha ha ! Too good man. ๐ Try to understand what I have done in my program(obviously something wrong would be there) and then you would understand yourself what changes you need to do in enqueue() and dequeue() methods accordingly for all cases.hiocan you help me with the other ,,
thanks,, -
hiook ,, ok ,, I will ,,
if you don't mind see what i have dine until now :d
public class ArrayQueue2
is't wright :d
{
private Object Q[];
private int front,rear;
private int size;
public ArrayQueue2(int capacity)
{
Q = new Object [capacity];
front = 0;
rear = -1;
}
public void enqueue (Object o)
{
if (front == ( rear + 2 ) % Q.length)
{
System.out.println("full quene");
}
else
{
rear = ( rear + 1 )% Q.length;
Q[rear] = o;
}
}
public Object dequene()
{
if (isEmpty())
{
System.out.println("empty quene");
return null;
}
else
{
Object o = Q[front];
front = ( front + 1 ) % Q.length;
return o;
}
}
public int size()
{
return ( Q.length + rear - front + 1 ) % Q.length;
}
public Object front()
{
if (this.isEmpty())
{
System.out.println("Empty queue");
return null;
}
else
{
return Q[front];
}
}
public boolean isEmpty()
{
return size() == 0;
}
} -
shalini_goel14Superb hio,
but why you are using size variable? Where you are setting its value ? You can remove that I guess.
Why you are setting rear=-1 ? -
hioThanks,,
I didn't notice the rear in the first answer :d
Since you asked me about the rear why you set it as rear = capacity;?? ,,
and the size i will deal with it ๐ -
shalini_goel14
Man, if we see the literal meaning of rear, it means end and your setting it to -1 means you are putting it before front. Anyways it is going to affect much.hioThanks,,
I didn't notice the rear in the first answer :d
Since you asked me about the rear why you set it as rear = capacity;?? ,,
and the size i will deal with it ๐
Go the way you want to go.๐ -
hioOK Bhot THANK you mera dost
You are reading an archived discussion.
Related Posts
While this is by no means the first time we've seen a robotic snake prototype, it isn't every day that we find one so close to deployment. This self-propelling, two...
Ok, I have been in a few seminars in college 5 to be all and many in school
In school I had to do what teachers said but in college...
OK friends I am gonna face the end of college on 23rd which is the date of my last exam
I am looking for a job for a few years...
Help Me Making a coil with AC voltage and 1GHz freq possible????
I am having a wild idea for creating a new energy source but well I am still pretty...
heya friends
I have been here from a pretty long time but never introduced myself:shifty:
Its Kashish Malhotra
I am 18 years old.
I am an Electrical Engineering Student(final year...