![]() ![]() This example demonstrates how you can implement a simple generic queue using a LinkedList. Returns the number of items in the queue Returns true if the queue is empty, false otherwise Returns the item at the front of the queue without removing it Throw new IllegalStateException("Queue is empty") Removes an item from the front of the queue and returns it Private LinkedList list = new LinkedList() Here’s an example of a simple queue implementation in Java using LinkedList: In the main method, I created a queue of Integer and showcased basic enqueue, dequeue, and peek operations. Queue is empty.This example demonstrates how to implement a simple generic queue using a LinkedList. 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 |