


Let us define our class template for the Queue implementation.įront and rear instance variable are the linked list node that holds the data and a reference to the next node. Please note that the Java implementation of the priority queue uses different names for the methods. once we remove the tail then there is no way to point to the element that was before a tail in a singly linked list unless we have an extra pointer. Removal of an element happens from the front of the Q and if the linked list tail is the front of a queue then how we going to remove the front element of a Q(the tail of linked list) as the linked list does not allow to remove the tail of it at a constant time O(1). The front of Q is the head and rear of the Q is tail due to the remove operation. You can visit my previous article for custom implementation of linked list in Java.

While using linked list for the queue implementation, EnQueue operation is implemented by inserting element at the end of the list and DeQueue operation is implemented by deleting an element from the beginning of the list. Every operation uses extra space and time to deal wih references.Every operation takes constant time O(1).Below are the advantages of using linked list for queue implementation: We will be using linked list for the implementation of our custom queue. IsEmpty(): Indicates whether no elements are stored in the queue or not. QueueSize(): Returns the number of elements stored in the queue. Queue OperationsĮnQueue(): Inserts an element at the end of the Queue.ĭeQueue(): Removes and returns the element at the front of the Queue.įront(): Returns the element at front without removing it. Hence, it is called First in First out(FIFO). The element which is inserted first is the first element that is deleted. What is QueueĪ queue is an ordered list in which insertions are done at one end(rear) and deletions are done at the other end(front). A priority queue is an abstract data type, it is like a regular queue or stack data structure, but where additionally each element has a priority associated with it. We will also check the time complexity of these different operations. In the course, we will perform different queue operations such as enQueue(), deQueue(), etc. In this article, we will create a custom implementation of Queue data structure in Java.
