This repository contains a Python implementation of various queue data structures, including fixed-size, dynamic, and circular queues. The implementation adheres to the FIFO (First-In-First-Out) principle and provides a wide range of operations for managing queues.
This project implements several types of queues in Python, including:
- Fixed-Size Queue: A queue with a predefined maximum size.
- Dynamic Queue: A queue that can grow dynamically as elements are added.
- Circular Queue: A queue that uses a circular buffer to efficiently manage space.
- Circular Dynamic Queue: A combination of circular and dynamic queues, allowing for efficient space management and dynamic growth.
The implementation is designed to be flexible, allowing for various data types and providing methods for common queue operations such as enqueue, dequeue, merge, reverse, and more.
- Multiple Queue Types: Supports fixed-size, dynamic, and circular queues.
- Type Safety: Ensures that all elements in the queue are of the same type.
- Efficient Operations: Optimized for common queue operations like
enqueue,dequeue, andmerge. - Iterable: Queues can be iterated over using Python's
forloop. - Merge Operations: Supports merging queues with or without modifying the original queue.
- Ordering and Reversing: Provides methods to order or reverse the queue without modifying the original.
- Error Handling: Includes robust error handling for edge cases like empty queues or type mismatches.
- Defines the interface for all queue implementations.
- Methods:
enqueue(data): Adds an element to the queue.dequeue(): Removes and returns the first element from the queue.clear(): Clears the queue.merge_with_change(second): Merges another iterable into the queue, modifying the original.merge_without_change(second): Merges another iterable into a copy of the queue.traverse(): Prints the elements of the queue.__iter__(): Makes the queue iterable.size(): Returns the number of elements in the queue.is_empty(): Checks if the queue is empty.first(): Returns the first element without removing it.last(): Returns the last element without removing it.contains(value): Checks if a value exists in the queue.to_list(): Converts the queue to a list.copy(): Returns a deep copy of the queue.reverse(): Reverses the queue.order_with_change(): Orders the queue (ascending or descending).order_without_change(): Orders a copy of the queue.
- A fixed-size queue implementation using a dynamic list.
- Methods:
enqueue(data): Adds an element to the queue if there is space.dequeue(): Removes and returns the first element.merge_with_change(second): Merges another iterable into the queue.merge_without_change(second): Merges another iterable into a copy of the queue.reverse_without_change(): Reverses a copy of the queue.order_without_change(the_type): Orders a copy of the queue (ascending or descending).
- A dynamic queue implementation with no size limit.
- Methods:
- Similar to
array_fixed_queue, but without size constraints.
- Similar to
- A circular queue implementation with a fixed size.
- Methods:
enqueue(data): Adds an element to the queue, wrapping around if necessary.dequeue(): Removes and returns the first element.merge_with_change(second): Merges another iterable into the queue.merge_without_change(second): Merges another iterable into a copy of the queue.reverse_without_change(): Reverses a copy of the queue.order_without_change(the_type): Orders a copy of the queue (ascending or descending).
- A circular dynamic queue implementation with no size limit.
- Methods:
- Similar to
array_circular_fixed_queue, but with dynamic resizing.
- Similar to
To use the queue implementations, simply import the desired class from the collective_queue.py file:
from collective_queue import array_fixed_queue, array_dynamic_queue, array_circular_fixed_queue, arrary_circular_dynamic_queue
# Example: Fixed-size queue
fixed_queue = array_fixed_queue(limit=10, the_type=int)
fixed_queue.enqueue(5)
fixed_queue.enqueue(10)
print(fixed_queue.dequeue()) # Output: 5
# Example: Dynamic queue
dynamic_queue = array_dynamic_queue(the_type=int)
dynamic_queue.enqueue(20)
dynamic_queue.enqueue(30)
print(dynamic_queue.dequeue()) # Output: 20
# Example: Circular queue
circular_queue = array_circular_fixed_queue(limit=5)
circular_queue.enqueue(1)
circular_queue.enqueue(2)
print(circular_queue.dequeue()) # Output: 1from collective_queue import array_fixed_queue
queue = array_fixed_queue(limit=5, the_type=int)
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
print(queue.dequeue()) # Output: 1
print(queue.first()) # Output: 2
print(queue.last()) # Output: 3from collective_queue import array_dynamic_queue
queue1 = array_dynamic_queue(the_type=int)
queue1.enqueue(1)
queue1.enqueue(2)
queue2 = array_dynamic_queue(the_type=int)
queue2.enqueue(3)
queue2.enqueue(4)
merged_queue = queue1.merge_without_change(queue2)
print(merged_queue.queue()) # Output: [1, 2, 3, 4]Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.