0% found this document useful (0 votes)
45 views

Unit II Linear Data Structures

This document discusses linear data structures, specifically stacks and queues. It describes types of queues, including circular queues and priority queues. Circular queues are described as an extension of linear queues that connects the last node to the first to form a circular link, resolving the memory wastage problem of linear queues. Priority queues are collections of elements stored according to priority level, with higher priority elements processed before lower priority ones. Implementations of circular queues and priority queues using arrays, linked lists, and heaps are discussed. Examples of applications like CPU scheduling and traffic systems are provided.

Uploaded by

Lavanya J
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Unit II Linear Data Structures

This document discusses linear data structures, specifically stacks and queues. It describes types of queues, including circular queues and priority queues. Circular queues are described as an extension of linear queues that connects the last node to the first to form a circular link, resolving the memory wastage problem of linear queues. Priority queues are collections of elements stored according to priority level, with higher priority elements processed before lower priority ones. Implementations of circular queues and priority queues using arrays, linked lists, and heaps are discussed. Examples of applications like CPU scheduling and traffic systems are provided.

Uploaded by

Lavanya J
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIT II LINEAR DATA STRUCTURES

– STACKS AND QUEUES

TYPES OF QUEUES-
CIRCULAR QUEUE
PRIORITY QUEUE
Why?
• Implementation of a linear queue brings the drawback of
memory wastage
• However, memory is a crucial resource that you should always
protect by analyzing all the implications while designing
algorithms or solutions.
• In the case of a linear queue, when the rear pointer reaches the
MaxSize of a queue, there might be a possibility that after a
certain number of dequeue() operations, it will create an empty
space at the start of a queue. 
• Additionally, this newly created empty space can never be re-
utilized as the rear pointer reaches the end of a queue. Hence,
experts introduced the concept of the circular queue to
overcome this limitation.
• As shown in the figure,
the rear pointer arrives at the beginning
of a queue with the help of a circular
link to re-utilize the empty space to
insert a new element.
• This simple addition of a circular
link resolves the problem of memory
wastage in the case of queue implementation. Thus, this
particular type of queue is considered the best version of a queue
data structure. 
What is Circular Queue in a Data Structure?

• A circular queue is an extended version of a linear queue as it


follows the First In First Out principle with the exception that
it connects the last node of a queue to its first by forming a
circular link.
• Hence, it is also
called a Ring Buffer.
• The circular queue
resolves the memory
wastage problem with
the help of a circular
link.
• Now you will understand how you can achieve circular
incrementation, with the help of an example.
• Let’s say the MaxSize of your queue is 5, and the rear pointer
has already reached the end of a queue.
• There is one empty space at the beginning of a queue, which
means that the front pointer is pointing to location 1.
Operations on Circular Queue: 
• Front: Get the front item from queue.
• Rear: Get the last item from queue.
• enQueue(value) This function is used to insert an element into the circular
queue. In a circular queue, the new element is always inserted at Rear position. 
– Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) ||
(rear == front-1)).
– If it is full then display Queue is full. If queue is not full then, check if (rear
== SIZE – 1 && front != 0) if it is true then set rear=0 and insert element.
• deQueue() This function is used to delete an element from the circular queue.
In a circular queue, the element is always deleted from front position. 
– Check whether queue is Empty means check (front==-1).
– If it is empty then display Queue is empty. If queue is not empty then step
3
– Check if (front==rear) if it is true then set front=rear= -1 else check if
(front==size-1), if it is true then set front=0 and return the element.
Insertion in Circular Queue
Step 1 : If FRONT = (REAR + 1) % MAXSIZE : then
Write : “Queue Overflow” and return.
[End of If structure]
Step 2 : Read NUM to be inserted in Circular Queue.
Step 3 : If FRONT= -1 : then
Set FRONT = REAR =0.
Else Set REAR=(REAR + 1) % MAXSIZE.
[End of If Else structure]
Step 4 : Set CQUEUE[REAR]=NUM;
Step 5 : Exit
Deletion of Circular Queue
Step 1 : If FRONT = - 1 : then
Write : “Queue Underflow” and return.
[End of If Structure]
Step 2 : Set NUM = CQUEUE[FRONT].
Step 3 : Write ‘Deleted element from circular queue is :
",NUM.
Step 4 : If FRONT = REAR : then
Set FRONT = REAR = -1;
Else Set FRONT = (FRONT + 1) % MAXSIZE.
Step 5 : Exit
Applications of Circular Queue
The circular Queue can be used in the following scenarios:
• Memory management: The circular queue provides memory
management. As we have already seen that in linear queue, the memory
is not managed very efficiently. But in case of a circular queue, the
memory is managed efficiently by placing the elements in a location
which is unused.
• CPU Scheduling: The operating system also uses the circular queue to
insert the processes and then execute them.
• Traffic system: In a computer-control traffic system, traffic light is one
of the best examples of the circular queue. Each light of traffic light gets
ON one by one after every interval of time. Like red light gets ON for
one minute then yellow light for one minute and then green light. After
green light, the red light gets ON.
PRIORITY QUEUE
USAGE SCENARIOS
A theatre with seats. The catch is that the first row alone has royal seats, while
the other rows have normal seats, The royal seats are for special or VIP
visitors. The normal seats, on the other hand, are for normal people. Here, all
the people enter in a queue, but only the prioritized members get the front
seats. The other people have to sit behind all the priority members. In the same
way, a priority queue maintains priority for each element. It works as a queue
with applied priority.
What is priority queue?
• A priority queue is a collection of elements where the elements
are stored according to their priority levels. The order in which
the elements get added or removed is decided by the priority
of the element.
• Following rules are applied to maintain a priority queue
(1) The element with a higher priority is processed before any element of
lower priority.
(2) If there are elements with the same priority, then the element added
first in the queue would get processed.
• Basically, in priority queues, the data does not follow the FIFO
order. The order of data is by the order of their priority. So, the
data gets into the queue, gets recorded based on priority, and
then comes out.
• Generally, the order of priority will be from lower to higher,
that is, element with priority value ‘0’ will come first. This can
be changed based on design.
• Priority queues are used for implementing job scheduling by
the operating system where jobs with higher priorities are to
be processed first.
• Another application of Priority queues is simulation systems
where priority corresponds to event times.
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.
• The access modes of the priority queue are the same
as normal queue. ‘Enqueue’ is used to send data unit
in, ‘Dequeue’ is used to take data unit out.
DEMO
• https://www.studytonight.com/code/
python/ds/priority-queue-in-python.php
# A simple implementation of Priority Queue if __name__ == '__main__':
# using Queue. myQueue = PriorityQueue()
class PriorityQueue(object): myQueue.insert(12)
def __init__(self): myQueue.insert(1)
self.queue = [] myQueue.insert(14)
myQueue.insert(7)
def __str__(self): print(myQueue)
return ' '.join([str(i) for i in self.queue]) while not myQueue.isEmpty():
print(myQueue.delete())
# for checking if the queue is empty
def isEmpty(self):
return len(self.queue) == 0

# for inserting an element in the queue


def insert(self, data):
self.queue.append(data)

# for popping an element based on Priority


def delete(self):
try:
max = 0
for i in range(len(self.queue)):
if self.queue[i] > self.queue[max]:
max = i
item = self.queue[max]
del self.queue[max]
return item
except IndexError:
print()
exit()
APPLICATIONS OF QUEUES :
1. Round Robin technique for processor scheduling is
implemented using queues.
2. All types of customer service (like railway ticket
reservation ) center software’s are designed using
queues to store customers information.

You might also like