0% found this document useful (0 votes)
18 views4 pages

1011dsa4 2

Uploaded by

vj62n68r68
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)
18 views4 pages

1011dsa4 2

Uploaded by

vj62n68r68
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/ 4

23CS2012-Data Structures and Algorithms URK23CS1044

Ex. No. 4
Array Implementation of Circular Queue
19/08/24

Aim:
To simulate the working of a Circular Queue of integers using an array with the
following operations: (a) Insert (b) Delete (c) Display

Description:
A circular queue is a linear data structure that operates on the First-In-First-Out (FIFO) principle,
where elements are added to the rear and removed from the front. Unlike a traditional linear
queue, a circular queue connects the end of the queue back to the beginning, forming a circular
structure. This means that when the rear of the queue reaches the end of the array, it wraps
around to the beginning if there is space available. This circular connection optimizes the use of
array space and ensures efficient management of elements as it prevents wasted space and allows
continuous use of the queue.

Algorithm:
1. Initialize the circular queue with a specified size, creating an array to hold the queue elements.
2. Set `front` to 0, `rear` to -1, and `count` to 0 for tracking the queue's current state.
3. Define the `is_full` method to check if the queue has reached its maximum capacity.
4. Define the `is_empty` method to check if the queue is currently empty.
5. Implement the `insert` method to add an item to the queue, updating the `rear` pointer
and incrementing the `count`.
6. Ensure that when the `rear` pointer is updated, it wraps around using the modulo operation
to maintain the circular nature.
7. If the queue is full, print an error message and prevent further insertions.
8. Implement the `delete` method to remove an item from the queue, updating the `front`
pointer and decrementing the `count`.
9. Ensure that when the `front` pointer is updated, it wraps around using the modulo operation
to maintain the circular nature.
10. If the queue is empty, print an error message and prevent further deletions.
11. Implement the `display` method to print the current contents of the queue in the correct order.
12. In the `display` method, iterate from `front` to `rear`, wrapping around as necessary
to account for the circular structure.

Ex No: 3 Array Implementation of Circular Queue


23CS2012-Data Structures and Algorithms URK23CS1044

13. Create a `menu` function to present the user with options to insert, delete, display, or exit
the program.
14. Implement the `main` function to initialize the queue, repeatedly display the menu,
and handle user input to perform the chosen operation.
15. Ensure that the program continues running until the user chooses to exit, at which point
it terminates gracefully.

Program:

class CircularQueue:
def init (self, size):
self.size = size
self.queue = [None] * size
self.front = 0
self.rear = -1
self.count = 0 # To keep track of the number of elements in the queue

def is_full(self):
return self.count == self.size

def is_empty(self):
return self.count == 0

def insert(self, item):


if self.is_full():
print("Queue is full. Cannot insert.")
else:
self.rear = (self.rear + 1) % self.size
self.queue[self.rear] = item
self.count += 1
print(f"Inserted {item} into the queue.")

def delete(self):
if self.is_empty():
print("Queue is empty. Cannot delete.")
else:
item = self.queue[self.front]
self.front = (self.front + 1) % self.size
self.count -= 1
print(f"Deleted {item} from the
queue.") return item

def display(self):
if self.is_empty():
print("Queue is empty.")
else:

Ex No: 3 Array Implementation of Circular Queue


23CS2012-Data Structures and Algorithms URK23CS1044

print("Queue contents:")
i = self.front
for _ in range(self.count):
print(self.queue[i], end=' ')
i = (i + 1) % self.size
print()

def menu():
print("1. Insert")
print("2. Delete")
print("3. Display")
print("4. Exit")
return input("Enter your choice: ")

def main():
print("URK23CS1033")
size = int(input("Enter the size of the queue: "))
queue = CircularQueue(size)

while True:
choice = menu()
if choice == '1':
item = int(input("Enter the item to insert: "))
queue.insert(item)
elif choice == '2':
queue.delete()
elif choice == '3':
queue.display()
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")

if name == " main ":


main()

Ex No: 3 Array Implementation of Circular Queue


23CS2012-Data Structures and Algorithms URK23CS1044

Input and Output Screenshots

Result:

The concept of application of stack of circular queue has been successfully studied and output
has been verified.

Ex No: 3 Array Implementation of Circular Queue

You might also like