A queue is a first-in first-out FIFO abstract data type that is heavily used in computing. Uses for queues involve anything where you want things to happen in the order that they were called, but where the computer can't keep up to speed.
For example:. In this queue form, elements are able to join the queue at one end and can exit from the queue at the other end. A real life example of this would be queuing to pay at the supermarket. The first person into the queue is the first person to be served. And if you join the queue last you are going to be served last no pushing! However, there is one difference, in a supermarket queue once the first person has been served the rest of the queue shuffles forward, making the previously second person now first, this is possible in a computer but very costly in terms of processing.
The linear queue you are about to see 'crawls' through memory, devouring everything it comes across. The most basic implementation of a linear queue would work its way through its allotted memory, without reusing memory. The front and end pointers can only move forwards and sooner or later would reach the end of the allotted memory - at this point, no more items could be added to the queue. Take a look at this example and the values of the two pointers storing the front of the queue FrontPtr and the next free memory location NextFree.
Even though slots before 3 are no longer needed, no more data can be added as NextFree has gone beyond the last slot. A better implementation would reuse memory by shifting each item one to the left, each time an item is removed from the front. However, if there are many items in the queue this could become very slow. See below note that we no longer need a front pointer as item 1 is always the front :. Generally, a circular buffer requires three pointers :.
The following example shows a partially-full fixed length buffer and how it handles additions and deletions from a queue. Take particular care to note the pointers. Circular queues are very common in computer science and they are used to store things, they have the following benefits. This is based on the order of priority of the elements in the queue. In other words, each element of this type of queue has different level of priority attached to it; e.
A real world example of this would be an Accident and Emergency Department waiting room, someone who had fallen over, would probably be low priority compared with someone who had cut their head open, and as such even if the head injury patient came in later they would probably jump the queue.
In a computer we use priority queues to handle the execution of processes. Same is the case with Queue data structure. Data inserted first, will leave the queue first. The process to add an element into queue is called Enqueue and the process of removal of an element from queue is called Dequeue. Queue, as the name suggests is used whenever we need to manage any group of objects in an order in which the first one coming in, also gets out first while the others wait for their turn, like in the following scenarios:.
Queue can be implemented using an Array , Stack or Linked List. The easiest way of implementing a queue is by using an Array. As we add elements to the queue, the tail keeps on moving ahead, always pointing to the position where the next element will be inserted, while the head remains at the first index.
When we remove an element from Queue, we can follow two possible approaches mentioned [A] and [B] in above diagram. In [A] approach, we remove the element at head position, and then one by one shift all the other elements in forward position. In approach [B] we remove the element from head position and then move head to the next position.
In approach [A] there is an overhead of shifting the elements one position forward every time we remove the first element.
In approach [B] there is no such overhead, but whenever we move head one position ahead, after removal of first element, the size on Queue is reduced by one space each time. To implement approach [A], you simply need to change the dequeue method, and include a for loop which will shift all the remaining elements by one position. Just like Stack, in case of a Queue too, we know exactly, on which position new element will be added and from where an element will be removed, hence both these operations requires a single step.
Learn CSS. Learn JavaScript. C Language C Tutorial. C Compiler. Standard Template Library. Python Python Tutorial. Python Programs. Python How Tos. Numpy Module. Matplotlib Module. Tkinter Module. We will explore this interesting example in more detail later. In addition to printing queues, operating systems use a number of different queues to control processes within a computer.
The scheduling of what gets done next is typically based on a queuing algorithm that tries to execute programs as quickly as possible and serve as many users as it can. Also, as we type, sometimes keystrokes get ahead of the characters that appear on the screen. This is due to the computer doing other work at that moment. The keystrokes are being placed in a queue-like buffer so that they can eventually be displayed on the screen in the proper order.
Social Runestone in social media:.
0コメント