Discussion Locked

Impement a queue class for integers

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I need help with the following application: I am in the process of developing two classes: Array and Linked list. A Client(main) should be able to use either of the two implementations transparently. The client will need to keep track of: 1.) The length of the queue, 2.) the number of times the dequeue method would be called when the queue is empty(I don't have to call the method, but skip the call, record the event and continue). 3.) Record if an "Array index out of bound" exception is reported by the enqueue method. The client(main) will decide whether to call the enqueue or the dequeue method by using a dice(0 for dequeue or 1 for enqueue using the random number generator). If the enqueue is to be used, a random integer in the rangeof 1-1000 should be generated. This loop should run at least 200 times.

3 replies

by mohamed_zafer

If ur application is using only Linkedlist then u can use the folloing implementation for the Queue class.

class Q
{
List ls = new LinkedList();
Boolean isEmpty()
{
return ls.size()==0;
}
Object front()
{
return ls.get(0);
}
void enqueue(Object item)
{
ls.add(item);
}
Object dequeue()
{
Object rs=front();
ls.remove(0);
return rs;
}
int size()
{
return ls.size();
}
}

U can use only Integer to store and not int.Add logic to this class to keep track of no: oftimes u call de/enQ(eg. add counter)

If u r using array then u have to create a new array each time u enqueue,'coz array size cannot be changed.

This discussion is closed to new replies.