Deque (LinkedList-based Double-ended Queue)
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 | |
Deque
Bases: IQueue[T]
A double-ended queue (deque) implementation.
Source code in src/datastructures/deque.py
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 | |
__contains__(item)
Checks if an item exists in the deque.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- item
|
T
|
The item to check for existence. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/datastructures/deque.py
112 113 114 115 116 117 118 119 120 121 122 | |
__eq__(other)
Compares two deques for equality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- other
|
Deque
|
The deque to compare with. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/datastructures/deque.py
124 125 126 127 128 129 130 131 132 133 134 | |
__init__(data_type=object)
Initializes the deque with a specified data type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- data_type
|
type
|
The type of data the deque will hold. |
required |
Source code in src/datastructures/deque.py
13 14 15 16 17 18 19 20 | |
__len__()
Returns the number of items in the deque.
Returns:
| Type | Description |
|---|---|
int
|
|
Source code in src/datastructures/deque.py
103 104 105 106 107 108 109 110 | |
__repr__()
Returns a detailed string representation of the deque.
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in src/datastructures/deque.py
151 152 153 154 155 156 157 158 | |
__str__()
Returns a string representation of the deque.
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in src/datastructures/deque.py
142 143 144 145 146 147 148 149 | |
back()
Returns the back item of the deque without removing it.
Returns:
| Type | Description |
|---|---|
T
|
|
Raises:
| Type | Description |
|---|---|
-IndexError
|
If the deque is empty. |
Source code in src/datastructures/deque.py
82 83 84 85 86 87 88 89 90 91 92 | |
clear()
Clears all items from the deque.
Source code in src/datastructures/deque.py
136 137 138 139 140 | |
dequeue()
Removes and returns the item from the front of the deque.
Returns:
| Type | Description |
|---|---|
T
|
|
Raises:
| Type | Description |
|---|---|
-IndexError
|
If the deque is empty. |
Source code in src/datastructures/deque.py
34 35 36 37 38 39 40 41 42 43 44 | |
dequeue_back()
Removes and returns the item from the back of the deque.
Returns:
| Type | Description |
|---|---|
T
|
|
Raises:
| Type | Description |
|---|---|
-IndexError
|
If the deque is empty. |
Source code in src/datastructures/deque.py
58 59 60 61 62 63 64 65 66 67 68 | |
empty()
Checks if the deque is empty.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/datastructures/deque.py
94 95 96 97 98 99 100 101 | |
enqueue(item)
Adds an item to the back of the deque.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- item
|
T
|
The item to add to the back of the deque. |
required |
Raises:
| Type | Description |
|---|---|
-TypeError
|
If the item is not of the correct type. |
Source code in src/datastructures/deque.py
22 23 24 25 26 27 28 29 30 31 32 | |
enqueue_front(item)
Adds an item to the front of the deque.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- item
|
T
|
The item to add to the front of the deque. |
required |
Raises:
| Type | Description |
|---|---|
-TypeError
|
If the item is not of the correct type. |
Source code in src/datastructures/deque.py
46 47 48 49 50 51 52 53 54 55 56 | |
front()
Returns the front item of the deque without removing it.
Returns:
| Type | Description |
|---|---|
T
|
|
Raises:
| Type | Description |
|---|---|
-IndexError
|
If the deque is empty. |
Source code in src/datastructures/deque.py
70 71 72 73 74 75 76 77 78 79 80 | |