310 likes | 885 Views
This presentation on Circular Queue in Data Structure will acquaint you with a clear understanding of circular queue implementation. In this presentation, you will understand array implementation and linked list implementation of circular queue using the C programming language. Finally, it will cover the applications of a circular queue to understand its importance. So, let's get started!<br><br>The topics covered in this Circular Queue In Data Structure slide are:<br>1. Introduction<br>2. Why was the Concept of Circular Queue Introduced?<br>3. Representation of Circular Queue Using Arrays and Linked Lists<br>
E N D
Implementation of Circular Queue Using Array / Linked List Representation of Circular Queue Using Arrays and Linked List AGENDA Why Was the Concept of Circular Queue Introduced? Applications of the Circular Queue
Why Circular Queue? In implementation of a linear queue, the drawback of memory wastage comes forward. Memory is a crucial resource that should be protected while designing Algorithms and solutions.
Queue in Data Structure In Linear queue insertion is done at the rear node and deletion at the front node. FRONT (Deletion) REAR (Insertion) Initially, when the queue is empty, both Front and Rear Node point to the null memory space, i.e., -1.
Queue in Data Structure As we keep adding elements into the array, Rear pointer reaches the end of the queue where front pointer remains at the same location. 7 -2 1 11 9 0 1 2 3 4 Rear Front
Queue in Data Structure When we perform Dequeue() operations, the empty space gets created at the front node of the queue. 7 -2 1 11 9 0 1 2 3 4 Rear Front Empty space created after performing dequeue() operation!
Introduction: Circular Queue A circular queue is extended version of linear queue with exception that last node of this queue is connected to its first, making a circular link. Circular Link 0 1 2 3 4 Rear Front
Understanding: Circular Queue Let’s understand how memory wastage problem is getting resolved with the help of an example of circular queue. Newly Created Empty Space FRONT REAR 0 4 5 7 9 -2 11 1 3 1 2
Representation of Circular Queue Using Array and Linked List
Circular Incrementation A circular queue works with the process of circular Incrementation, i.e., When we reach the end of queue, we start from the beginning of a queue. FRONT REAR 5 0 12 7 -6 1 4 -2 1 7 2 3
Circular Incrementation The rear pointer has reached the end of queue, but there is a empty space at the beginning of queue which can be utilized due to circular link. REAR 0 5 12 0 -6 1 4 Insertion of new element! 1 7 FRONT 2 3
Implementation of Circular incrementation How can we perform circular incrementation logically? Let’s consider the example that we have discussed previously: MaxSize of queue = 6 Rear = 5 and Front = 2 Rear pointer should be brought at location 0 now. Rear = Rear + 1 = 5 + 1 = 6 Rear = (Rear + 1) % 6 Rear = (5 + 1)% 6 = 0 With help of Modulus operation we can achieve circular incrementation!
Array Representation of Circular Queue The array representation of a circular queue is achieved using the method of circular incrementation. Circular Link 0 1 2 3 4 Rear Front
Insertion Scenarios in Circular Queue Case 1: Determining if the queue is full. 7 -2 1 11 9 0 1 2 3 4 If((Rear + 1)%5 == Front) Rear Front (4+1)%5 = 0 = Front Return, Overflow Error.
Insertion Scenarios in Circular Queue Case 2: When there is space at the beginning of a queue. New Insertion 5 1 11 9 0 1 2 3 4 Rear = (Rear + 1)%5 Rear Front Rear = (4+1)%5 = 0 Queue[Rear] = value
Linked List Representation of Circular Queue The linked list representation of a circular queue can be achieved by changing the reference field of tail node. Tail Head Add 1 Add 1 Add 2 Address Circular Link is Created!
Implementation of Circular Queue Using Array And Linked List
Coding Implementation Let’s formulate the code for implementation of circular queue using array and Linked list!
Let’s consider, MaxSize = 5, rear = 4, front =1 void Print() { int count = ((rear + MAX_SIZE - front) % MAX_SIZE)+1; int i; for (i = 0; i < count; i++) { printf("%d ", a[(front+i)%MAX_SIZE]); } printf("\n"); } 0 1 2 3 4 Count = ((4 + 5 – 1) % 5) + 1 = (8 % 5) +1 = 4 Rear Front We will read data elements using front pointer in range 0 to count.
Applications of the Circular Queue
Buffer in Operating Systems 01 Applications of the Circular Queue CPU Scheduling Algorithms 02 Computer Controlled Traffic Light Systems 03
Buffer in Operating Systems A buffer is a region of a physical memory storage which temporarily stores the data while it is being moved from one place to another. Enqueue Audio Input Data Buffer Dequeue Audio Output
Buffer in Video Streaming Netflix Servers Amazon Cloud Content Delivery Networks Content Delivery Network Audio Chunks Video Chunks Data Buffer: Receiver
CPU Scheduling Algorithm The circular queue is used for developing the Round Robin Scheduling algorithm.
CPU Scheduling Algorithm A Ready queue which is responsible for storing processes before resource allocation is implemented using a circular queue. Burst Time = 4 CPU Utilization time stamp = 2 µ sec P1 P2 P3 P4 P1 Ready Queue
Computer Controlled Traffic System The traffic systems that are controlled by computers utilizes circular queue to manage activation of signal lights.
Computer Controlled Traffic System Traffic lights will be lit in constant interval of time following FIFO principle. Control Queue Y R G