Pros, Cons, and Applications of Queue Structure Elements are added with a specific delay argument and are removed according to these delay times. The delay is added to the current time in order to calculate the expiration time. We include the “delay” argument to specify the time that passes before the item is dequeued. Heappush(self.queue, (expiration_time, item)) Priority Queue ImplementationĬreating a priority queue structure isn’t as hard as it might sound. To remove an element, we increment the front pointer by one to remove the first element. We increment the rear element by one and set the new element to the rear index to add an element. As such, we must check if the queue is full or empty when adding or removing elements respectively. Some of the key differences are that we define a specific capacity with the circular queue, and have to account for circular behavior. The process is similar, but the definitions of the operations are more elaborate. Return (self.rear - ont + self.capacity) % self.capacity + 1 Return (self.rear + 1) % self.capacity = ont Self.rear = (self.rear + 1) % self.capacity The following code gives a basic example of creating a linear queue. Next, let’s take a look at some simple examples of how queues are implemented, starting with linear queues. To remove an element, the front pointer is assigned to the next node. When adding an element, we point the rear node to the new node and point the new node to be the rear node. However, using a linked list is a little more complicated. When creating an array-based queue, we can add elements by incrementing the rear pointer by 1, or remove them by incrementing the front pointer. In these cases, the operations can’t be performed. Likewise, if we want to remove an element, we need to check if the queue is empty. If we want to add an element, we need to check if the queue is full. The latter stores elements in a node and uses front and rear pointers. Most queues can be implemented using either an array or a linked list. Most queue structures use the same basic operations. Instead of being removed, elements are de-prioritized.Įlements can be inserted and removed from both ends of the queue.Įlements are dequeued after a specific time has lapsed, rather than by the FIFO principle. Queue TypeĮlements are prioritized, meaning those with the highest priority are dequeue first. A brief overview of these is given in the table. The last position in the queue simply wraps around to the front position.Īlthough these are the most common queue types, there are many others, which don’t obey the FIFO principle. These kinds of queues don’t have a fixed front and rear, such as a traffic light system. This would be an example of a linear queue, but we can also have circular queues. This is similar to a real-life queue, where the first person in the queue is the first person to leave the queue. There are many types of queues, but most of them operate on the First-In-First-Out (FIFO) principle. What Is the Queue Structure and How Does It Work? In this article, we’ll explain the queue structure and illustrate it with examples. Understanding how the different kinds of queues work is an essential part of learning about data structures. Queues are a useful data structure in programming for modeling both technical and real-life situations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |