Display: Prints all the items in the queue.īefore we see how to implement this with code, you need to understand how the enqueue and dequeue operations work and how they affect the front and back positions.Front/ Peek: Returns the value of the item in front of the queue without dequeuing (removing) the item.Dequeue: Removes an item from the front of the queue.Enqueue: Adds an item from the back of the queue.The following operations are commonly used in a queue: Whichever structure you go with, always remember that insertion of items happens through the back and deletion through the front. Note that you can reverse the structure of your queue – you can have the front on the right and the back on the left side. There are terms used for the insertion and deletion of items in a queue which we will cover in the next section. The items are inserted through the back and removed through the front. The image shows an array with various cells. Here is a diagram to help you understand better: The back is where the items are inserted and the front is the part of the queue where items are removed/deleted. For the purpose of clarity and consistency, we will stick to using front and back. Structure of a QueueĪ queue is mainly made up of two parts: the front/head and the rear/tail/back. They are helped in the order which they came. Once one individual is attended to, they leave the queue for the next person to be attended to. Using a real world example, we can compare a queue data structure to a queue of individuals standing in line for a service. You can also say that items are removed in the order they were inserted. This implies that the first item to be inserted will be the first to be removed. What Is a Queue?Ī queue is linear data structure that consists of a collection is of items that follow a first-in-first-out sequence. In this article, we will talk about the queue data structure, its operations, and how to implement these operations using an array in Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |