![]() ![]() The value of both the Rear and Front is set to -1 initially and then these values are incremented or decremented as the elements are inserted and deleted. Hence we can say that the Queue's working principle is based on First-In-First-Out(FIFO).Ī Queue is a linear data structure in which elements are inserted from one end called the Rear and are removed from the other end called the Front. Whoever is first, is going to get the ticket first. Queue is empty.The concept of a Queue data structure is similar to the queue we come across in our day-to-day life like at a bus stop.Īnd you have to wait until your number arrives, right? creating the object of the class to call the methods ("Below given are the elements of the Queue") įor (i = front i != rear i = (i + 1) % SIZE) Displaying the elements of the Circular queue Incrementing the front in a circular manner if there is only one element in the queue ("Element " + value + " is inserted successfully") incrementing the rear position in circular manner using modulo operator No more elements could be inserted in it") Implementing the 2 scenarios to check if the queue is full or not creating the constructor of the above class 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:.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.The following scenarios need to be kept in mind while working in the circular queue: 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |