REPORT WRITING
REPORT WRITING
Queue
A Queue is defined as a linear data structure that is open at both ends and the
operations are performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end,
and all deletions from the list are made at the other end. The element which is first
pushed into the order, the operation is first performed on that.
Characteristics of Queue:
Queue can handle multiple data.
We can access both ends.
They are fast and flexible.
Circular queue
A circular queue is the extended version of a regular queue where the last element is connected
to the first element. Thus forming a circle-like structure.
circular queue, cyclic buffer or ring buffer is a data structure that uses a single,
fixed-size buffer as if it were connected end-to-end. This structure lends itself
easily to buffering data streams. There were early circular buffer implementations
in hardware.
The circular queue solves the major limitation of the normal queue. In a normal
queue, after a bit of insertion and deletion, there will be non-usable empty space.
Assume that 1 is written in the center of a circular buffer (the exact starting
location is not important in a circular buffer):
Then assume that two more elements are added to the circular buffer — 2 &
3 — which get put after 1:
If two elements are removed, the two oldest values inside of the circular
buffer would be removed. Circular buffers use FIFO (first in, first out)
logic. In the example, 1 & 2 were the first to enter the circular buffer,
they are the first to be removed, leaving 3 inside of the buffer.
If the buffer has 7 elements, then it is completely full:
1.insert Operation
check if the queue is full
for the first element, set value of FRONT to 0
circularly increase the REAR index by 1 (i.e. if the rear reaches the end, next it
would be at the start of the queue)
add the new element in the position pointed to by REAR
2. Delete Operation
check if the queue is empty
return the value pointed by FRONT
circularly increase the FRONT index by 1
for the last element, reset the values of FRONT and REAR to -1
However, the check for full queue has a new additional case:
Case 1: FRONT = 0 && REAR == SIZE - 1
Case 2: FRONT = REAR + 1
Priority queue
A priority queue is a special type of queue in which each element is associated
with a priority value. And, elements are served on the basis of their priority. That
is, higher priority elements are served first.
However, if elements with the same priority occur, they are served according to
their order in the queue.
Implementation
Priority queue can be implemented using an array, a linked list, a heap data
structure, or a binary search tree. Among these data structures, heap data structure
provides an efficient implementation of priority queues.
insert(node)
{
list.append(node)
}
pull()
{
highest = list.get_first_element()
foreach node in list
{
if highest.priority < node.priority
{
highest = node
}
}
list.remove(highest)
return highest
}
In another case, one can keep all the elements in a priority sorted list (O(n)
insertion sort time), whenever the highest-priority element is requested, the first
one in the list can be returned.
insert(node)
{
foreach (index, element) in list
{
if node.priority < element.priority
{
list.insert_at_index(node,index)
break
}
}
}
pull()
{
highest = list.get_at_index(list.length-1)
list.remove(highest)
return highest
}