0% found this document useful (0 votes)
31 views9 pages

REPORT WRITING

Uploaded by

rashmimaruthi2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views9 pages

REPORT WRITING

Uploaded by

rashmimaruthi2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

REPORT WRITING

OVERVIEW OF THE PROJECT

Our project is based on the topic “ APPLICATION ON QUEUE ON TRAFFIC


MANAGEMENT ” we have designed our PPT based on circular queue.

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.

FIFO Principle of Queue:


 A Queue is like a line waiting to purchase tickets, where the first person in line
is the first person served. (i.e. First come first serve).
 Position of the entry in a queue ready to be served, that is, the first entry that
will be removed from the queue, is called the front of the
queue(sometimes, head of the queue), similarly, the position of the last entry in
the queue, that is, the one most recently added, is called the rear (or the tail) of
the queue. See the below figure.

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.

How Circular Queue Works


Circular Queue works by the process of circular increment i.e. when we try to
increment the pointer and we reach the end of the queue, we start from the
beginning of the queue.
A circular buffer first starts out empty and has a set length. In the diagram below is
a 7-element buffer:

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:

A property of the circular buffer is that when it is full and a


subsequent write is performed, then it starts overwriting the oldest
data. In the current example, two more elements — A & B — are
added and they overwrite the 3 & 4:

Alternatively, the routines that manage the buffer could


prevent overwriting the data and return an error or raise
an exception. Whether or not data is overwritten is up to the
semantics of the buffer routines or the application using the
circular buffer.
Finally, if two elements are now removed then what would be
returned is not 3 & 4 but 5 & 6 because A & B overwrote the
3 & the 4 yielding the buffer with:

Circular Queue Operations


The circular queue work as follows:
 two pointers FRONT and REAR
 FRONT track the first element of the queue
 REAR track the last elements of the queue
 initially, set value of FRONT and REAR to -1

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

Applications of Circular Queue


 CPU scheduling
 Memory management
 Traffic Management

Algorithm for circular queue


Insertion
1. Insert CircularQueue ( )
2.
3. 1. If (FRONT == 1 and REAR == N) or (FRONT == REAR + 1) Then
4.
5. 2. Print: Overflow
6.
7. 3. Else
8.
9. 4. If (REAR == 0) Then [Check if QUEUE is empty]
10.
11. (a) Set FRONT = 1
12.
13. (b) Set REAR = 1
14.
15. 5. Else If (REAR == N) Then [If REAR reaches end if QUEUE]
16.
17. 6. Set REAR = 1
18.
19. 7. Else
20.
21. 8. Set REAR = REAR + 1 [Increment REAR by 1]
22.
23. [End of Step 4 If]
24.
25. 9. Set QUEUE[REAR] = ITEM
26.
27. 10. Print: ITEM inserted
28.
29. [End of Step 1 If]
30.
31. 11. Exit

Deletion of the circular queue


1. Delete CircularQueue ( )
2.
3. 1. If (FRONT == 0) Then [Check for Underflow]
4.
5. 2. Print: Underflow
6.
7. 3. Else
8.
9. 4. ITEM = QUEUE[FRONT]
10.
11. 5. If (FRONT == REAR) Then [If only element is left]
12.
13. (a) Set FRONT = 0
14.
15. (b) Set REAR = 0
16.
17. 6. Else If (FRONT == N) Then [If FRONT reaches end if QUEUE]
18.
19. 7. Set FRONT = 1
20.
21. 8. Else
22.
23. 9. Set FRONT = FRONT + 1 [Increment FRONT by 1]
24.
25. [End of Step 5 If]
26.
27. 10. Print: ITEM deleted
28.
29. [End of Step 1 If]
30.
31. 11. Exit
32.

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.

Priority Queue Operations


Basic operations of a priority queue are inserting, removing, and peeking elements.

1) Insertion in a Priority Queue


When a new element is inserted in a priority queue, it moves to the empty slot
from top to bottom and left to right. However, if the element is not in the correct
place then it will be compared with the parent node. If the element is not in the
correct order, the elements are swapped. The swapping process continues until all
the elements are placed in the correct position.
2) Deletion in a Priority Queue
As you know that in a max heap, the maximum element is the root node. And it
will remove the element which has maximum priority first. Thus, you remove the
root node from the queue. This removal creates an empty slot, which will be
further filled with new insertion. Then, it compares the newly inserted element
with all the elements inside the queue to maintain the heap invariant.
3) Peek in a Priority Queue
This operation helps to return the maximum element from Max Heap or the
minimum element from Min Heap without deleting the node from the priority
queue.

Working of the priority queue

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
}

You might also like