Circular Queue | Set 2 (Circular Linked List Implementation)
Prerequisite – Circular Singly Linked List Show
We have discussed basics and how to implement circular queue using array in set 1. In this post another method of circular queue implementation is discussed, using Circular Singly Linked List. Operations on Circular Queue:
Circular QueueWhy was the concept of the circular queue introduced?There was one limitation in the array implementation of Queue. If the rear reaches to the end position of the Queue then there might be possibility that some vacant spaces are left in the beginning which cannot be utilized. So, to overcome such limitations, the concept of the circular queue was introduced. As we can see in the above image, the rear is at the last position of the Queue and front is pointing somewhere rather than the 0th position. In the above array, there are only two elements and other three positions are empty. The rear is at the last position of the Queue; if we try to insert the element then it will show that there are no empty spaces in the Queue. There is one solution to avoid such wastage of memory space by shifting both the elements at the left and adjust the front and rear end accordingly. It is not a practically good approach because shifting all the elements will consume lots of time. The efficient approach to avoid the wastage of the memory is to use the circular queue data structure. What is a Circular Queue?A circular queue is similar to a linear queue as it is also based on the FIFO (First In First Out) principle except that the last position is connected to the first position in a circular queue that forms a circle. It is also known as a Ring Buffer. Operations on Circular QueueThe following are the operations that can be performed on a circular queue: Applications of Circular QueueThe circular Queue can be used in the following scenarios: Enqueue operationThe steps of enqueue operation are given below: Scenarios for inserting an elementThere are two scenarios in which queue is not full: There are two cases in which the element cannot be inserted: Algorithm to insert an element in a circular queue Step 1: IF (REAR+1)%MAX = FRONT Step 2: IF FRONT = -1 and REAR = -1 Step 3: SET QUEUE[REAR] = VAL Step 4: EXIT Dequeue OperationThe steps of dequeue operation are given below: Algorithm to delete an element from the circular queue Step 1: IF FRONT = -1 Step 2: SET VAL = QUEUE[FRONT] Step 3: IF FRONT = REAR Step 4: EXIT Let's understand the enqueue and dequeue operation through the diagrammatic representation. Implementation of circular queue using ArrayOutput: Implementation of circular queue using linked listAs we know that linked list is a linear data structure that stores two parts, i.e., data part and the address part where address part contains the address of the next node. Here, linked list is used to implement the circular queue; therefore, the linked list follows the properties of the Queue. When we are implementing the circular queue using linked list then both the enqueue and dequeue operations take O(1) time. Output: Next TopicDS Deque ← prev next → Circular Queue using Linked List in C ProgrammingFebruary 6, 2022 Circular Linked List is little more complicated linked data structure. In the circular linked list we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements points to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required. Application of Circular Linked ListImplementing Circular Linked ListImplementing a circular linked list is very easy and almost similar to linear linked list implementation, with the only difference being that, in circular linked list the last Node will have it's next point to the Head of the List. In Linear linked list the last Node simply holds NULL in it's next pointer. So this will be oue Node class, as we have already studied in the lesson, it will be used to form the List. class Node { public: int data; //pointer to the next node node* next; node() { data = 0; next = NULL; } node(int x) { data = x; next = NULL; } }Circular Linked ListCircular Linked List class will be almost same as the Linked List class that we studied in the previous lesson, with a few difference in the implementation of class methods. class CircularLinkedList { public: node *head; //declaring the functions //function to add Node at front int addAtFront(node *n); //function to check whether Linked list is empty int isEmpty(); //function to add Node at the End of list int addAtEnd(node *n); //function to search a value node* search(int k); //function to delete any Node node* deleteNode(int x); CircularLinkedList() { head = NULL; } }Insertion at the BeginningSteps to insert a Node at beginning : Insertion at the EndSteps to insert a Node at the end : Searching for an Element in the ListIn searhing we do not have to do much, we just need to traverse like we did while getting the last node, in this case we will also compare the data of the Node. If we get the Node with the same data, we will return it, otherwise we will make our pointer point the next Node, and so on. node* CircularLinkedList :: search(int x) { node *ptr = head; while(ptr != NULL && ptr->data != x) { //until we reach the end or we find a Node with data x, we keep moving ptr = ptr->next; } return ptr; }Deleting a Node from the ListDeleting a node can be done in many ways, like we first search the Node with data which we want to delete and then we delete it. In our approach, we will define a method which will take the data to be deleted as argument, will use the search method to locate it and will then remove the Node from the List. To remove any Node from the list, we need to do the following : Recommended: Please try your approach on {IDE} first, before moving on to the solution.Below is the implementation of above approach: C++
Circular Queue in Data Structure: Overview, Implementation Using Array and Linked ListBy SimplilearnLast updated on Feb 7, 20229944Table of ContentsView MoreIt is common to use circular queues in a data structure in operating systems. It is used to manage the execution of computing processes or programs. You use a circular queue as a buffer to store the processes in order of their insertion and then remove them at the time of resource allocation or execution. In this tutorial, you will explore a circular queue in a data structure along with its implementation and applications. 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 above, 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. Full Stack Web Developer CourseTo become an expert in MEAN StackView CourseQueue using circular linked listWrite aC Program to implement queue using circular linked list.Here’s simpleProgram to implement queue using circular linked list in C Programming Language. |