Linear and Non-Linear data structures. A data structure is said to be linear if the elements form a sequence, for example, Array, Linked list, queue etc. Elements in a nonlinear data structure do not form a sequence, for example, Tree, Hash tree, Binary tree, etc.
What is Queue?
In general, a line or sequence of people or vehicles awaiting their turn to be attended to or to proceed and a list of data items, commands, etc., stored so as to be retrievable in a definite order, usually the order of insertion.
In the data structure, a queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle.
Enqueue The process to add an element to the queue, means to insert an item into the entrance of the queue.
Dequeue The process of removal of an element from the queue, means removing through Exit item.
The picture demonstrates the FIFO access.
The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
Another Queue representation
Figure 1 Queue
Explaining the First-In-First-Out methodology we can understand from diagram given below.
The queue is also an abstract data type or a linear data structure, in which the first element is inserted from one end called REAR (also called tail), and the deletion of existing element takes place from the other end called as FRONT (also called head), can be seen in the diagram below.
Basic features of Queue
Like Stack, Queue is also an ordered list of elements of similar data types.
The queue is a FIFO (First in First Out) structure.
Once a new element is inserted into the Queue, all the elements inserted before the new element in the queue must be removed, to remove the new element.
Peek ( ) − function is often used to return the value of the first element without dequeuing it or Gets the element at the front of the queue without removing it.
isfull() − Checks if the queue is full.
isempty() − Checks if the queue is empty.
Example Peek ( )
isempty () Algorithm
Applications of Queue
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:
Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
In real life scenario, Call Center phone systems use Queues to hold people calling them in an order, until a service representative is free.
Handling of interrupts in real-time systems. The interrupts are handled in the same order as they arrive i.e. First come first served.
Implementation of Queue
The queue can be implemented using an Array, Stack or Linked List. The easiest way of implementing a queue is by using an Array. Initially the head (FRONT) and the tail (REAR) of the queue points at the first index of the array (starting the index of the array from 0). 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.
Steps for Queue
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow message and exit.
Step 3 − If the queue is not full, increment the rear pointer to point the next empty space.
Step 4 − Add data element to the queue location, where the rear is pointing.
Step 5 − return success
Figure 2 adding a new Element in Queue (enqueue operation)
Algorithm for enqueue operation
Accessing data from the queue is a process of two tasks − access the data where the front is pointing and remove the data after access. The following steps are taken to perform dequeue operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow message and exit.
Step 3 − If the queue is not empty, access the data where the front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
Step 5 − Return success.
Figure 3 Dequeue
Algorithm for dequeue operation
Please understand this process carefully as explained in lecture. Given below in Figure 4
Figure 4 Enqueue and Dequeue
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 forwarding position. In approach [B] we remove the element from head position and then move the 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 the first element, the size of Queue is reduced by one space each time.
Key terms (Just for Knowledge)
A queue in which the first item added is always the first one out.
A queue in which the items are sorted so that the highest priority item is always the next one to be extracted.
Life critical systems
Systems on which we depend for safety and which may result in death or injury if they fail: medical monitoring, industrial plant monitoring and control and aircraft control systems are examples of life critical systems.
Real time systems
Systems in which time is a constraint. A system which must respond to some event (e.g. the change in attitude of an aircraft caused by some atmospheric event like wind-shear) within a fixed time to maintain stability or continue correct operation (e.g. the aircraft systems must make the necessary adjustments to the control surfaces before the aircraft falls out of the sky!).
In computer science, a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority.
Heap is a special tree-based data structure that satisfies the following special heap properties
A heap is a complete binary tree – all levels are fully filled, except possibly the bottom level, which may be partially filled from left to right
Difference between Complete and incomplete tree is given below.
All nodes are either [greater than or equal to] or [less than or equal to] each of its children. If the parent nodes are greater than their child nodes, heap is called a Max-Heap, and if the parent nodes are smaller than their child nodes, heap is called Min-Heap.
Min-Heap − Where the value of the root node is less than or equal to either of its children.
Figure 5 for Input → 35 33 42 10 14 19 27 44 26 31
Max-Heap − Where the value of the root node is greater than or equal to either of its children. (Gif is also available in folder)
Figure 6 for Input → 35 33 42 10 14 19 27 44 26 31
Max Heap Construction Algorithm
Max Heap Deletion Algorithm
Let us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap always happens at the root to remove the Maximum (or minimum) value.
Please see the max_heap_animation.gif to understand properly