The use of the queue interface is given in the above sections. It is also given how FIFO is used in the queue interface. It is used to extend the collection interfaces. In the above-given article, Queue Interface is described clearly. The above-given program’s output will produce an error as inserting an element of String type in the Integer type Queue is not supported. In this example, trying to add the String type of element in the Integer type restricted Queue. ("\nSpecified item " + intVar2 + " is not available in the Queue.") ("\nSpecified item is available in the Queue.") condition to check if element is available in the queue In this example, we can see how restricted types of elements can be added to the Queue. items available in the queue after applying poll method ("\nHead item of the Queue: " + qObj.peek()) items available in the queue after applying peek method ("\nHead item of the Queue: " + qObj.element()) ("\nTotal item count in Queue: " + qObj.size()) The next two lines display available items in the queue, head item of the queue, respectively.Remove method is used further for removing the element from the Queue.Items in the queue, the head element of the queue are displayed in the next line.In the next statement, the size of the queue is displayed using the size() method.Further, different string elements are added in the queue by using the add method.An instance of the Queue is created of type LinkedList.This example shows how different methods are used in the program & what these methods are returning. size(): This method returns the count of the element available in the queue.Įxamples to Implement Java Queue Interfaceīelow are the examples of implementing java queue interface: Example #1.If the element is found to be empty, it creates exceptions. remove(): This method used to remove & return the front element of the queue.If the queue is empty, then it returns null. poll(): This method removes an element from the front of the queue & returns the front element from the queue.If the queue is empty, then it returns NULL peek(): peek() method is almost similar to the element() method.The only difference between add() and the offer is that if the add method cannot add the element in the queue, it creates an exception while the offer method doesn’t. offer(): This method is also used to insert elements in the queue.In the example section, we can see how the element() method works. If the queue is empty, then it generates exceptions. element(): element() is one of the important methods that returns the queue head element.It returns true if the response is successful otherwise, it generates exceptions. add(): add() method used to insert elements in the queue.Some of the commonly used methods of queue interface are given below Queue q = new LinkedList() Queue Interface Methods Queue instances with the restricted data type can be created using the following given syntax. In the below two Queue syntax, LinkedList implementation is the standard one. Special behavior is needed when the rear reaches the end of the array.In the following syntax, An object obj is instantiated using the LinkedList / PriorityQueue class.Array implementation requires less space.Array implementation has limited capacity because of using a fixed size array.Void enqueue ( int value ) Comparing queue using linked list and queue using array Let’s write the code for enqueue operation using the concepts given above. You can see that the new element is added at the beginning of the array and the pointer ‘rear’ is changed to 0. Here, the new element on the enqueue operation will be added to the first element of the array i.e., the element having 0 index. There is one more case in the enqueue operation when the rear is the last element of the array and the queue is not full. We just need to add an element at the index ‘rear+1’ and increase the value of the ‘rear’ and size by 1 for the enqueue operation. The next and the most important operations on a stack are enqueue and dequeue. We will change these values according to our need after the initialization of the queue. We have made the values of ‘front’, ‘rear’ and ‘size’ -1 because the queue is not yet initialized. #include #define MAXSIZE 10 int queue int front = - 1 int rear = - 1 int size = - 1
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |