|
7 | 7 | "[Back to Content](../../../content.md)\n",
|
8 | 8 | "\n",
|
9 | 9 | "# Queue\n",
|
10 |
| - "The queue data structure is a linear dynamic set of entities that are kept in a specificied sequence. It can be modified by adding entities, at one end of the structure, and removing them from the other end. Thus, a queue implements a **FIFO** policy (i.e., _first-in, first-out_). In other words, the first element added to the queue will be the first element to be removed.\n", |
| 10 | + "The queue data structure is a linear dynamic set of entities in a specific sequence. It can be modified by adding entities, at one end of the structure, and removing them from the other. Thus, a queue implements a **FIFO** policy (i.e., _first-in, first-out_). In other words, the first element added to the queue will be the first element to be removed.\n", |
11 | 11 | "\n",
|
12 |
| - "In the queue, elements are added at the end of the sequence, and removed at the beginning. By convention, the end is also known as _back_, _rear_, or **tail** (we will stick to this term), and the beginning is also known as _front_, or **head** (we will stick to this term).\n", |
| 12 | + "In the queue, elements are added at the end of the sequence, and removed at the beginning. By convention, the end is also known as *back*, *rear*, or ***tail*** (we will stick to this term), and the beginning is known as *front* or ***head*** (we will stick to this term).\n", |
13 | 13 | "\n",
|
14 |
| - "The queue has three main operation: insertion (i.e., enqueue), deletion (i.e., dequeue), and peek.\n", |
| 14 | + "The queue has three main operations: insertion (i.e., enqueue), deletion (i.e., dequeue), and peek.\n", |
15 | 15 | "\n",
|
16 |
| - "Unlike other structures, such as arrays or linked lists, the queue cannot be accesed via index (not even to search if the queue has a value). You would have to delete and get the element as many times as you need. However, this can be synthetically done with an auxiliary buffer to avoid modifications.\n", |
| 16 | + "Unlike other structures, such as arrays or linked lists, the queue cannot be accessed via index (not even to search if the queue has a value). You would have to delete and get the element as many times as you need. However, this can be synthetically done with an auxiliary buffer to avoid modifications.\n", |
17 | 17 | "\n",
|
18 | 18 | "\n",
|
19 | 19 | "\n",
|
|
78 | 78 | "To solve this nuance we can use a Queue wrapper that keeps track of the head and tail adding and removing elements in and out of the set.\n",
|
79 | 79 | "\n",
|
80 | 80 | "## The Queue (Wrapper)\n",
|
81 |
| - "Let's use a basic implementation of the Queue wrapper to see how using it avoids to lose track of the head unless we actually delete an element of the queue.\n", |
| 81 | + "Let's use a basic implementation of the Queue wrapper to see how using it avoids losing track of the head unless we actually delete an element of the queue.\n", |
82 | 82 | "\n",
|
83 | 83 | "**Note:** I'll be changing the `Queue` implementation to explain better in a concise manner each operation. The full implementation of the [`QueueNode`](./QueueNode.js), the [`Queue`](./Queue.js), the [`QueueNode` and `Queue` tests](./__test__/queue.spec.js) can be checked for further analysis."
|
84 | 84 | ]
|
|
184 | 184 | "cell_type": "markdown",
|
185 | 185 | "metadata": {},
|
186 | 186 | "source": [
|
187 |
| - "Again, despite of having lost the track of the nodes in the `head` variable, the queue is still intact because the wrapper has the track of the head.\n", |
| 187 | + "Again, despite having lost track of the nodes in the `head` variable, the queue is still intact because the wrapper has the track of the head.\n", |
188 | 188 | "\n",
|
189 | 189 | "## Operations\n",
|
190 | 190 | "### `queue.enqueue`\n",
|
191 |
| - "\n", |
| 191 | + "\n", |
192 | 192 | "\n",
|
193 |
| - "**Enqueue** is the insertion operation. The insertion occurs at the end of the queue. When the queue is empty the insertion add a queue node and the wrapper tracks it in the head. As the node is the only element in the queue it has been tracked in the *head* but it's also the *tail*.\n", |
| 193 | + "**Enqueue** is the insertion operation. The insertion occurs at the end of the queue. When the queue is empty the insertion adds a queue node and the wrapper tracks it in the head. As the node is the only element in the queue it has been tracked in the *head* but in the *tail* as well.\n", |
194 | 194 | "\n",
|
195 | 195 | "However, as more elements are enqueued to the set, the tail keeps pointing to the tail.\n",
|
196 | 196 | "\n",
|
197 |
| - "Enqueueing has $O(1)$ time complexity because the node is added always at the end of the queue.\n", |
| 197 | + "Enqueueing has $O(1)$ time complexity because the node is always added at the end of the queue.\n", |
198 | 198 | "\n",
|
199 | 199 | "### `queue.dequeue`\n",
|
200 | 200 | "\n",
|
201 | 201 | "\n",
|
202 |
| - "**Dequeue** is the deletion operation. The deletion occurs at the beginning of the queue. When the queue is empty the deletion is not performed. On the other hand, when the queue has elements the *head* reference goes to its current *next* and the *head* is returned to the caller. This has no impact on the *tail* reference.\n", |
| 202 | + "**Dequeue** is the deletion operation. The deletion occurs at the beginning of the queue. When the queue is empty the deletion is not performed. On the other hand, when the queue has elements the *head* reference goes to its current *next*, and the *head* is returned to the caller. This has no impact on the *tail* reference.\n", |
203 | 203 | "\n",
|
204 | 204 | "Dequeueing has $O(1)$ time complexity too because the *head* reference is moved to the next node."
|
205 | 205 | ]
|
|
294 | 294 | "metadata": {},
|
295 | 295 | "source": [
|
296 | 296 | "### `queue.peek`\n",
|
297 |
| - "The `peek` function looks at the head of the queue without popping it out from the queue. With this operation we always have a way to look at the head value. No matter how many elements keep being enqueued, this operation will always look to the *head*." |
| 297 | + "The `peek` function looks at the head of the queue without popping it out from the queue. With this operation, we always have a way to look at the head value. No matter how many elements are enqueued, this operation will always look to the *head*." |
298 | 298 | ]
|
299 | 299 | },
|
300 | 300 | {
|
|
363 | 363 | "metadata": {},
|
364 | 364 | "source": [
|
365 | 365 | "## Runtime Complexity Overview\n",
|
366 |
| - "| Operation | Runtime Complexixty |\n", |
| 366 | + "| Operation | Runtime Complexity |\n", |
367 | 367 | "|:---------:|:-------------------:|\n",
|
368 | 368 | "| Enqueue | $O(1)$ |\n",
|
369 | 369 | "| Dequeue | $O(1)$ |\n",
|
|
0 commit comments