Queues in java pdf
Queue can also be defined as the list or collection in which the insertion is done from one end known as the rear end or the tail of the queue, whereas the deletion is done from another end known as the front end or the head of the queue. The real-world example of a queue is the ticket queue outside a cinema hall, where the person who enters first in the queue gets the ticket first, and the last person enters in the queue gets the ticket at last.
Similar approach is followed in the queue in data structure. In Linear Queue, an insertion takes place from one end while the deletion occurs from another end. The end at which the insertion takes place is known as the rear end, and the end at which the deletion takes place is known as front end. It strictly follows the FIFO rule. The major drawback of using a linear Queue is that insertion is done only from the rear end. If the first three elements are deleted from the Queue, we cannot insert more elements even though the space is available in a Linear Queue.
In this case, the linear Queue shows the overflow condition as the rear is pointing to the last element of the Queue. In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue except that the last element of the queue is connected to the first element.
It is also known as Ring Buffer, as all the ends are connected to another end. The representation of circular queue is shown in the below image -. The drawback that occurs in a linear queue is overcome by using the circular queue. If the empty space is available in a circular queue, the new element can be added in an empty space by simply incrementing the value of rear. The main advantage of using the circular queue is better memory utilization. It is a special type of queue in which the elements are arranged based on the priority.
It is a special type of queue data structure in which every element has a priority associated with it. Suppose some elements occur with the same priority, they will be arranged according to the FIFO principle. The representation of priority queue is shown in the below image -. Insertion in priority queue takes place based on the arrival, while deletion in the priority queue occurs based on the priority.
Priority queue is mainly used to implement the CPU scheduling algorithms. In Deque or Double Ended Queue, insertion and deletion can be done from both ends of the queue either from the front or rear. It means that we can insert and delete elements from both front and rear ends of the queue.
Deque can be used as a palindrome checker means that if we read the string from both ends, then the string would be the same. Deque can be used both as stack and queue as it allows the insertion and deletion operations on both ends. Deque can be considered as stack because stack follows the LIFO Last In First Out principle in which insertion and deletion both can be performed only from one end.
And in deque, it is possible to perform both insertion and deletion from one end, and Deque does not follow the FIFO principle. JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services.
Consider using a PriorityQueue when you want to take advantages of natural ordering and fast adding elements to the tail and fast removing elements at the head of the queue. Consider using an ArrayDeque when you want to utilize features of a double ended queue without list-based ones simpler than a LinkedList. Consider using an ArrayBlockingQueue when you want to use a simple blocking queue that has limited capacity bounded.
Elements added to this queue must implement the Delayed interface. That means an element can only be taken from the head of the queue when its delay has expired. Notify me of follow-up comments. Java Queue Collection Tutorial and Examples. E poll Retrieves and removes the head of this queue, or returns null if this queue is empty. E remove Retrieves and removes the head of this queue.
Methods inherited from interface java. Collection addAll , clear , contains , containsAll , equals , hashCode , isEmpty , iterator , parallelStream , remove , removeAll , removeIf , retainAll , size , spliterator , stream , toArray , toArray Methods inherited from interface java. Iterable forEach Method Detail add boolean add E e Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
When using a capacity-restricted queue, this method is generally preferable to add E , which can fail to insert an element only by throwing an exception. Parameters: e - the element to add Returns: true if the element was added to this queue, else false Throws: ClassCastException - if the class of the specified element prevents it from being added to this queue NullPointerException - if the specified element is null and this queue does not permit null elements IllegalArgumentException - if some property of this element prevents it from being added to this queue remove E remove Retrieves and removes the head of this queue.
This method differs from poll only in that it throws an exception if this queue is empty. Returns: the head of this queue Throws: NoSuchElementException - if this queue is empty poll E poll Retrieves and removes the head of this queue, or returns null if this queue is empty.
Returns: the head of this queue, or null if this queue is empty element E element Retrieves, but does not remove, the head of this queue.
This method differs from peek only in that it throws an exception if this queue is empty. Returns: the head of this queue Throws: NoSuchElementException - if this queue is empty peek E peek Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
0コメント