A

and to utilize these positions and to insert new values again we make the circular queue.

to keep track of the position of the enqueue and dequeue elements we use two variables front and count. initially, we set front and count variables to zero. and then if we enqueue any element in the queue then we increment count variable's value by 1 and when we dequeue an element from the queue we increment the front variable's value by 1.

we use this formula to find the position where we need to insert a new value.

here the length of the queue is equal to the size of the queue.

and we use this formula to find the position from where we need to delete the element from the queue.

here first we need to store the value None into the front position of the queue. and then we use the formula to find the new position of the front.

**circular queue**is a queue in which the front and end parts are connected together and make a circle.## Why we use a Circular queue?

In the queue when we delete the item from the front part then the position from where we deleted the values become vacant or None.and to utilize these positions and to insert new values again we make the circular queue.

**To make a circular queue we need to give a fixed size to it.**__Note:__### Enqueue operation

to insert a new value in the queue is called the enqueue operation.### Dequeue operation

to delete a value from the queue is called the Dequeue operation.to keep track of the position of the enqueue and dequeue elements we use two variables front and count. initially, we set front and count variables to zero. and then if we enqueue any element in the queue then we increment count variable's value by 1 and when we dequeue an element from the queue we increment the front variable's value by 1.

we use this formula to find the position where we need to insert a new value.

**i = ( front + count ) % len( queue )**

here the length of the queue is equal to the size of the queue.

and we use this formula to find the position from where we need to delete the element from the queue.

**queue[front] = None**

**front = ( front + 1 ) % len( queue )**

here first we need to store the value None into the front position of the queue. and then we use the formula to find the new position of the front.

#### Program to Implement a circular queue using array in a python programming language.

class EmptyQueueError(Exception): pass class Queue: def __init__(self, default_size=10): self.items = [None] * default_size self.front = 0 self.count = 0 def is_empty(self): return self.count == 0 def size(self): return self.count def enqueue(self, item): if self.count == len(self.items): self.resize(2 * len(self.items)) i = (self.front + self.count) % len(self.items) self.items[i] = item self.count += 1 def dequeue(self): if self.is_empty(): raise EmptyQueueError("Queue is empty") x = self.items[self.front] self.items[self.front] = None self.front = (self.front + 1) % len(self.items) self.count -= 1 return x def peek(self): if self.is_empty(): raise EmptyQueueError("Queue is empty") return self.items[self.front] def display(self): print(self.items) def resize(self, newsize): old_list = self.items self.items = [None] * newsize i = self.front for j in range(self.count): self.items[j] = old_list[i] i = (1 + i) % len(old_list) self.front = 0 ########### if __name__ == '__main__': qu = Queue(6) while True: print("1. Enqueue") print("2. Dequeue") print("3. Peek") print("4. Size") print("5. Display") print("6. Quit") choice = int(input("Enter your choice : ")) if choice == 1: x = int(input("Enter the element : ")) qu.enqueue(x) elif choice == 2: x = qu.dequeue() print("Element deleted from the queue is : ", x) elif choice == 3: print("Element at the front end is ", qu.peek()) elif choice == 4: print("Size of queue ", qu.size()) elif choice == 5: qu.display() elif choice == 6: break else: print("Wrong choice") print()

#### Also, read these posts

- What are Data Structures and algorithms
- Algorithm design and analysis
- Classification of algorithms
- How to calculate the running time of an algorithm.
- Worst Average and Best-case analysis of the algorithm.
- Big o notation
- Big o notation examples
- Linked List in Data Structures
- Traversing in Linked list
- Operations on the linked list
- Insertion in the linked list
- Deletion in a linked list
- Reversing a linked list
- Sorting a linked list
- Find and remove the loop in the linked list
- Doubly linked list
- Insertion in the doubly linked list
- Deletion in the doubly linked list
- Reversing a doubly linked list
- Circular linked list
- Insertion in the circular linked list
- Deletion in the circular linked list
- Merge two linked list
- Header linked list
- Sorted linked list
- Stack in data structures
- Queue in data structures
- Dequeue in the data structure
- Priority queue
- Polish notation
- Tree in the data structure
- Binary tree
- Array representation of the binary tree
- linked representation of a binary tree
- Traversing in the binary tree
- Inorder traversal in the binary tree
- Preorder traversal in the binary tree
- Postorder traversal in the binary tree
- Level order traversal in the binary tree
- Binary search tree
- Insertion in the binary search tree
- Deletion in the binary search tree
- Heap in data structures

- What are Data Structures and algorithms
- Algorithm design and analysis
- Classification of algorithms
- How to calculate the running time of an algorithm.
- Worst Average and Best-case analysis of the algorithm.
- Big o notation
- Big o notation examples
- Linked List in Data Structures
- Traversing in Linked list
- Operations on the linked list
- Insertion in the linked list
- Deletion in a linked list
- Reversing a linked list
- Sorting a linked list
- Find and remove the loop in the linked list
- Doubly linked list
- Insertion in the doubly linked list
- Deletion in the doubly linked list
- Reversing a doubly linked list
- Circular linked list
- Insertion in the circular linked list
- Deletion in the circular linked list
- Merge two linked list
- Header linked list
- Sorted linked list
- Stack in data structures
- Queue in data structures
- Dequeue in the data structure
- Priority queue
- Polish notation
- Tree in the data structure
- Binary tree
- Array representation of the binary tree
- linked representation of a binary tree
- Traversing in the binary tree
- Inorder traversal in the binary tree
- Preorder traversal in the binary tree
- Postorder traversal in the binary tree
- Level order traversal in the binary tree
- Binary search tree
- Insertion in the binary search tree
- Deletion in the binary search tree
- Heap in data structures

## 0 Comments