Build and use your own minimal queue class using an array of type String of fixe
ID: 3861372 • Letter: B
Question
Build and use your own minimal queue class using an array of type String of fixed size to hold the queue.
The array should have the default size of 5.
The array size should be setable via a constructor.
The following methods must be implemented:
enqueue – inserts a value at the rear of the queue
dequeue – returns the value at the front of the queue, removing the value from the queue
isEmpty – returns true if the queue is empty, false otherwise
isFull - – returns true if the queue is full, false otherwise
Tip: In a queue of 2 or more elements, the index of the rear is “larger” than the index of the front – EXCEPT, the queue will need to “wrap around” the array. For example: If the array is 20 elements. the queue front is at index 15 and the queue rear is at index 19, the next enqueue will be at index 20, which doesn't exist. The index must “wrap around” the array, i. e. the next enqueue will be at index 0. The solution is to modulus by the array size whenever increasing an index, either rear of front. For example, to change the index of the rear to the next index,use the formula
rear = (rear + 1) % arraySize;
Explanation / Answer
Lets start implementing queue via circular queue.
Now,In queue we have to maintain two pointers front say f and rear say r. Front will point to the first(index 1) element i.e. head of the queue and dequeue operation will be happened from front pointer whereas Rear pointer will point at the last indexed element and enqueue will happen at rear.
Now for your understanding, lets write pseudo code first. Here r=rear,f=front, N=arraysize.
isFull() : if( r - f == -1 || N-1) then
return true
else return flase
isempty(): if(r==f) then
return true.
enqueue() :
if(usFull()) then
throw someuserdefinedQueueexception
else
Queue[r] = stringobj
r = (r+1) mod N
So lets start implementing in JAVA.
Step1) Make an Interface "Queue"
Step2) Write the exceptions
Step3) Write java class to implement the queue interface that we wrote in step1
Step 4:) Write class to test all this above classes.
Note, put all the above classes in same package, its a good practice.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.