Displaying elements of the Queue along with the position of front and rear.If rear!= size-1, then the new value can be inserted as the rear can be incremented further using a mod(size).īelow given is the example showing the implementation of Circular Queue in the Java program: Returns: This method returns the element at the front of the container or the head of the Queue.The queue is not full if the front!= 0 and rear = max-1, which means there are positions at the starting of the queue which are empty, so insertions can take place.The queue is full if front = rear+1, which means if, after so many deletions, the front is at position 3 of the queue and rear after insertions in a circular manner is at position 2, the list is full and no more insertions can take place.So the queue is full and no insertion can take place. The queue is full if front = 0 and rear = size -1, which means that the front is pointing to the first position and the rear at the last.Enqueue: To enqueue an element x into the queue, do the following: Increment rear by 1. Initialize two variables front and rear to -1. The following scenarios need to be kept in mind while working in the circular queue: Implement Circular Queue using Array: Initialize an array queue of size n, where n is the maximum number of elements that the queue can hold. If there are elements in the queue, the value pointed out by front is deleted and the circularly increases the front index by 1.front = rear, then delete the element and set front = rear = -1. Mean of the front= rear= -1, means the queue is empty, deletion cannot be performed. First, it is checked whether the queue is empty or not.if it reaches the end and the queue becomes full, it points to the start of the queue. The rear is incremented in a circular way, i.e.The new element is added at the position of the rear.On inserting the new element in the queue, the rear becomes rear + 1.If we insert the first element in the Queue, both the Front and Rear are set to 0.Initially, the value of the Front and Rear is set to -1.As the basic nature of the Queue, deletion of elements takes place from the front of the Queue.īelow given are the steps followed while creating, inserting, or deleting the elements in the Circular Queue: Dequeue(): This function is used to delete the element of the Queue.Elements in the Queue are inserted at the rear position. Enqueue(value): It is a function that is used to insert the element on the queue.Rear: It is used to point to the last element of the queue.Front: It is used to point to the first element of the queue. * * the generic type of each item in this queue * Robert Sedgewick * Kevin Wayne */ public class Queue implements Iterable Ĭopyright © 2000–2022, Robert Sedgewick and Kevin Wayne. * * For additional documentation, see * Section 4.3 of * Computer Science: An Interdisciplinary Approach * by Robert Sedgewick and Kevin Wayne. The enqueue, dequeue, peek, * size, and is-empty operations all take constant time * in the worst case. * * This implementation uses a singly-linked list with a nested class for * linked-list nodes. ****************************************************************************** * Compilation: javac Queue.java * Execution: java Queue enqueue and dequeue * operations, along with methods for peeking at the top item, testing if the * queue is empty, getting the number of items in the queue, and iterating * over the items in FIFO order. Below is the syntax highlighted version of Queue.java
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |