![]() In show() method, if queue is not empty, then a loop is run from’ front’ to ‘rear’ to show all the elements of queue from beginning till end.Īny queries regarding the logic used can be asked. And in this case both are again set to -1,to indicate an empty queue. If so, it means that all elements have been deleted making front become equal to rear. If it is true then appropriate message is displayed to show that no element is there to be deleted otherwise the first element pointed to by front is returned and front is incremented to point to the next element in queue. numbers. Here, we have used the LinkedList class that implements the Queue interface. In dequeue() method, the isEmpty(0 method is called to check for empty queue. Run Code Output Queue: 1, 2, 3 Removed Element: 1 Queue after deletion: 2, 3 In the above example, we have used the Queue interface to implement the queue in Java. It is also checked that if ‘front’ is -1, (showing empty queue) then it is also increased by 1 to point to the first element in the queue. If no, proper message is displayed otherwise an element is added by incrementing ‘rear’ because elements are added to the queue from rear end. In enqueue() method, isFull() method is first called to check whether there is space in the array or not. This whole job is placed in a do-while loop to prompt the user to repeat the work. ![]() depending upon the choice of the user, switch- case is used to select the correct method. ![]() The main() method displays a menu to the user giving choice to add an element to queue, remove an element from queue, display all the elements from queue or to exit from the program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |