CircularQueue (Array-based)
Download Source Files NEW
- Download and place the following in your datastructures folder:
- Download and place the following in your tests folder:
Specification
IQueue
Bases: Generic[T]
Interface for a queue data structure
Source code in src/datastructures/iqueue.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
__contains__(item)
abstractmethod
Returns True if the item is in the queue, False otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
T -- The item to search for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the item is in the queue, False otherwise. |
Source code in src/datastructures/iqueue.py
68 69 70 71 72 73 74 75 76 77 78 | |
__eq__(other)
abstractmethod
Compares two queues for equality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
object -- The other queue to compare. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
bool -- True if the queues are equal, False otherwise. |
Source code in src/datastructures/iqueue.py
80 81 82 83 84 85 86 87 88 89 90 | |
__len__()
abstractmethod
Returns the number of items in the queue.
Returns:
| Type | Description |
|---|---|
int
|
int -- The number of items in the queue. |
Source code in src/datastructures/iqueue.py
45 46 47 48 49 50 51 52 | |
__repr__()
abstractmethod
Returns a string representation of the queue.
Returns:
| Type | Description |
|---|---|
str
|
str -- A string representation of the queue. |
Source code in src/datastructures/iqueue.py
101 102 103 104 105 106 107 108 | |
__str__()
abstractmethod
Returns a string representation of the queue.
Returns:
| Type | Description |
|---|---|
str
|
str -- A string representation of the queue. |
Source code in src/datastructures/iqueue.py
92 93 94 95 96 97 98 99 | |
back()
abstractmethod
Returns the back item in the queue without removing it.
Returns:
| Type | Description |
|---|---|
T
|
T -- The back item in the queue. |
Source code in src/datastructures/iqueue.py
36 37 38 39 40 41 42 43 | |
clear()
abstractmethod
Clears the queue.
Source code in src/datastructures/iqueue.py
63 64 65 66 | |
dequeue()
abstractmethod
Dequeues an item from the queue.
Returns:
| Type | Description |
|---|---|
T
|
T -- The item dequeued from the queue. |
Source code in src/datastructures/iqueue.py
18 19 20 21 22 23 24 25 | |
empty()
abstractmethod
Returns True if the queue is empty, False otherwise.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the queue is empty, False otherwise. |
Source code in src/datastructures/iqueue.py
54 55 56 57 58 59 60 61 | |
enqueue(item)
abstractmethod
Enqueues an item into the queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
T -- The item to enqueue. |
required |
Source code in src/datastructures/iqueue.py
9 10 11 12 13 14 15 16 | |
front()
abstractmethod
Returns the front item in the queue without removing it.
Returns:
| Type | Description |
|---|---|
T
|
T -- The front item in the queue. |
Source code in src/datastructures/iqueue.py
27 28 29 30 31 32 33 34 | |
CircularQueue
Bases: IQueue[T]
Represents a fixed-size circular queue. The queue is circular in the sense that the front and rear pointers wrap around the array when they reach the end. The queue is full when the rear pointer is one position behind the front pointer. The queue is empty when the front and rear pointers are equal. This implementation uses a fixed-size array.
Source code in src/datastructures/circularqueue.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
empty
property
Returns True if the queue is empty, False otherwise
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.empty
True
>>> q.enqueue(1)
>>> q.empty
False
>>> q.enqueue(2)
>>> q.empty
False
>>> q.enqueue(3)
>>> q.empty
False
>>> q.enqueue(4)
>>> q.empty
False
>>> q.enqueue(5)
>>> q.empty
False
>>> q.dequeue()
1
>>> q.empty
False
>>> q.dequeue()
2
>>> q.empty
False
>>> q.dequeue()
3
>>> q.empty
False
>>> q.dequeue()
4
>>> q.empty
False
>>> q.dequeue()
5
>>> q.empty
Returns:
| Type | Description |
|---|---|
bool
|
True if the queue is empty, False otherwise |
front
property
Returns the item at the front of the queue without removing it
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.enqueue(1)
>>> q.enqueue(2)
>>> q.enqueue(3)
>>> q.front
1
>>> q.dequeue()
1
>>> q.front
2
>>> q.dequeue()
2
>>> q.front
3
>>> q.dequeue()
3
>>> q.front
IndexError('Queue is empty')
Returns:
| Type | Description |
|---|---|
T
|
The item at the front of the queue |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the queue is empty |
full
property
Returns True if the queue is full, False otherwise
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.full
False
>>> q.enqueue(1)
>>> q.full
False
>>> q.enqueue(2)
>>> q.full
False
>>> q.enqueue(3)
>>> q.full
False
>>> q.enqueue(4)
>>> q.full
False
>>> q.enqueue(5)
>>> q.full
True
>>> q.dequeue()
1
>>> q.full
False
>>> q.dequeue()
2
>>> q.full
False
>>> q.dequeue()
3
>>> q.full
False
>>> q.dequeue()
4
>>> q.full
False
>>> q.dequeue()
5
>>> q.full
False
Returns:
| Type | Description |
|---|---|
bool
|
True if the queue is full, False otherwise |
maxsize
property
Returns the maximum size of the queue
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.maxsize
5
Returns:
| Type | Description |
|---|---|
int
|
The maximum size of the queue |
__eq__(other)
Returns True if this CircularQueue is equal to another object, False otherwise
Equality is defined as
- The element values at the front and rear pointers are equal
- The element values between the front and rear pointers are equal
- The maxsize of the queue is equal
- The data_type of the queue is equal
- Two queues are equal if they have the same elements in the same order, regardless of the index of the front and rear pointers.
Examples:
>>> q1 = CircularQueue(maxsize=5, data_type=int)
>>> q2 = CircularQueue(maxsize=5, data_type=int)
>>> q1 == q2
True
>>> for i in range(5): q1.enqueue(i)
>>> for i in range(5): q2.enqueue(i)
>>> q1 == q2
True
>>> q1.dequeue()
0
>>> q1 == q2
False
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
The object to compare this CircularQueue to |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this CircularQueue is equal to another object, False otherwise |
Source code in src/datastructures/circularqueue.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
__init__(maxsize=0, data_type=object)
Initializes the CircularQueue object with a maxsize and data_type.
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.maxsize
5
>>> q.empty
True
>>> q.full
False
>>> q.front
IndexError('Queue is empty')
>>> q.rear
IndexError('Queue is empty')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maxsize
|
int
|
The maximum size of the queue |
0
|
data_type
|
The type of the elements in the queue |
object
|
Source code in src/datastructures/circularqueue.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
__len__()
Returns the number of items in the queue
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> len(q)
0
>>> q.enqueue(1)
>>> len(q)
1
>>> q.enqueue(2)
>>> len(q)
2
>>> q.enqueue(3)
>>> len(q)
3
>>> q.dequeue()
1
>>> len(q)
2
>>> q.dequeue()
2
>>> len(q)
1
>>> q.dequeue()
3
>>> len(q)
0
Returns:
| Type | Description |
|---|---|
int
|
The number of items in the queue |
Source code in src/datastructures/circularqueue.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
__repr__()
Returns a developer string representation of the CircularQueue object
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.enqueue(1)
>>> q.enqueue(2)
>>> q.enqueue(3)
>>> repr(q)
'ArrayQueue([1, 2, 3])'
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the CircularQueue object |
Source code in src/datastructures/circularqueue.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
__str__()
Returns a string representation of the CircularQueue
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.enqueue(1)
>>> q.enqueue(2)
>>> q.enqueue(3)
>>> print(q)
[1, 2, 3]
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the queue |
Source code in src/datastructures/circularqueue.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
clear()
Removes all items from the queue
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.enqueue(1)
>>> q.enqueue(2)
>>> q.enqueue(3)
>>> q.clear()
>>> q.empty
True
>>> q.front
IndexError('Queue is empty')
>>> q.rear
IndexError('Queue is empty')
Source code in src/datastructures/circularqueue.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
dequeue()
Removes and returns the item at the front of the queue
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.enqueue(1)
>>> q.enqueue(2)
>>> q.enqueue(3)
>>> q.dequeue()
1
>>> q.dequeue()
2
>>> q.dequeue()
3
>>> q.dequeue()
IndexError('Queue is empty')
>>> q.dequeue()
IndexError('Queue is empty')
Returns:
| Type | Description |
|---|---|
T
|
The item at the front of the queue |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the queue is empty |
Source code in src/datastructures/circularqueue.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
enqueue(item)
Adds an item to the rear of the queue
Examples:
>>> q = CircularQueue(maxsize=5, data_type=int)
>>> q.enqueue(1)
>>> q.enqueue(2)
>>> q.enqueue(3)
>>> q.front
1
>>> q.rear
3
>>> q.enqueue(4)
>>> q.enqueue(5)
>>> q.full
True
>>> q.enqueue(6)
IndexError('Queue is full')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to add to the queue |
required |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the queue is full |
Source code in src/datastructures/circularqueue.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |