Java Program To Implement Circular Queue Adt Using An Array In Java
A queue program in Java: This program gives an array-based circular queue. The index variables rp ('rear pointer') and fp ('front pointer') are confusing and require. Java Program To Implement Circular Queue Adt Using An Array To. Generic internal error number for program exceptions. PDK- Java release 2.
Disclaimer: I'm not a Java guy. • This If needs some breathing space. Public void enQueue(T value) { if ((rear+1)%size==front) { It would be much more readable like this: if ((rear + 1)% size == front) { • Same goes for this statement later in the same enQueue method. Download De The Vampire Diaries 3 Temporada Legendado. Rear=(rear+1)%size; • Actually, it looks like you could extract a function there.
• deQueue also suffers from inconsistent (and poor) whitespace. • I know it's just an example implementation to show us that it works, but QueueImpl is a poor name. There's no reason to abbrv names. It's not the 1960's. We're not counting bytes in our names anymore. Prefer clear, concise, and meaningful names over shortened ones.
• Speaking of the example implementation, it would be better to write an actual Unit Test than to print to the console. Fire Cad Software Free Download. The size field is not required (just use queue.length) In fact, the size field is a misnomer, because it is the capacity, not the size.
The size is the number of elements that the queue actually contains, while the capacity is the maximum number of elements that it can hold. In enQueue, you have a special case for the empty queue, and in deQueue you have a special case for the singleton queue. These special cases are avoidable. Instead of representing the empty queue with head == rear == -1 you could keep a separate size field (to remember how many elements are in the queue).
Or you could use a different invariant: queue empty iff head == rear queue full iff rear == (head+1)% queue.length.
Your implementation may not be the most efficient way of using an array to implement a queue, but given that you've decided to implement it this way, your enqueue method is a bit more complex than it need be. You have a field sz that can be used to determine where the new entry has to be put rather than checking through to find the first non-null. And the search for a non-null won't work properly anyway as the dequeue isn't clearing the last element moved forward.
The sz field will also tell you when you need to resize, rather than detecting that need by an exception.