mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-09 13:51:48 +08:00
build
This commit is contained in:
@@ -18,14 +18,14 @@ The common operations in a double-ended queue are listed below, and the names of
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
| Method Name | Description | Time Complexity |
|
||||
| ------------- | --------------------------- | --------------- |
|
||||
| Method Name | Description | Time Complexity |
|
||||
| ------------- | -------------------------- | --------------- |
|
||||
| `pushFirst()` | Add an element to the head | $O(1)$ |
|
||||
| `pushLast()` | Add an element to the tail | $O(1)$ |
|
||||
| `popFirst()` | Remove the first element | $O(1)$ |
|
||||
| `popLast()` | Remove the last element | $O(1)$ |
|
||||
| `peekFirst()` | Access the first element | $O(1)$ |
|
||||
| `peekLast()` | Access the last element | $O(1)$ |
|
||||
| `pushLast()` | Add an element to the tail | $O(1)$ |
|
||||
| `popFirst()` | Remove the first element | $O(1)$ |
|
||||
| `popLast()` | Remove the last element | $O(1)$ |
|
||||
| `peekFirst()` | Access the first element | $O(1)$ |
|
||||
| `peekLast()` | Access the last element | $O(1)$ |
|
||||
|
||||
</div>
|
||||
|
||||
@@ -360,7 +360,7 @@ Recall from the previous section that we used a regular singly linked list to im
|
||||
|
||||
For a double-ended queue, both the head and the tail can perform enqueue and dequeue operations. In other words, a double-ended queue needs to implement operations in the opposite direction as well. For this, we use a "doubly linked list" as the underlying data structure of the double-ended queue.
|
||||
|
||||
As shown in the Figure 5-8 , we treat the head and tail nodes of the doubly linked list as the front and rear of the double-ended queue, respectively, and implement the functionality to add and remove nodes at both ends.
|
||||
As shown in Figure 5-8, we treat the head and tail nodes of the doubly linked list as the front and rear of the double-ended queue, respectively, and implement the functionality to add and remove nodes at both ends.
|
||||
|
||||
=== "LinkedListDeque"
|
||||
{ class="animation-figure" }
|
||||
@@ -2264,7 +2264,7 @@ The implementation code is as follows:
|
||||
|
||||
### 2. Implementation based on array
|
||||
|
||||
As shown in the Figure 5-9 , similar to implementing a queue with an array, we can also use a circular array to implement a double-ended queue.
|
||||
As shown in Figure 5-9, similar to implementing a queue with an array, we can also use a circular array to implement a double-ended queue.
|
||||
|
||||
=== "ArrayDeque"
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -6,7 +6,7 @@ comments: true
|
||||
|
||||
"Queue" is a linear data structure that follows the First-In-First-Out (FIFO) rule. As the name suggests, a queue simulates the phenomenon of lining up, where newcomers join the queue at the rear, and the person at the front leaves the queue first.
|
||||
|
||||
As shown in the Figure 5-4 , we call the front of the queue the "head" and the back the "tail." The operation of adding elements to the rear of the queue is termed "enqueue," and the operation of removing elements from the front is termed "dequeue."
|
||||
As shown in Figure 5-4, we call the front of the queue the "head" and the back the "tail." The operation of adding elements to the rear of the queue is termed "enqueue," and the operation of removing elements from the front is termed "dequeue."
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -14,7 +14,7 @@ As shown in the Figure 5-4 , we call the front of the queue the "head" and the b
|
||||
|
||||
## 5.2.1 Common operations on queue
|
||||
|
||||
The common operations on a queue are shown in the Table 5-2 . Note that method names may vary across different programming languages. Here, we use the same naming convention as that used for stacks.
|
||||
The common operations on a queue are shown in Table 5-2. Note that method names may vary across different programming languages. Here, we use the same naming convention as that used for stacks.
|
||||
|
||||
<p align="center"> Table 5-2 Efficiency of queue operations </p>
|
||||
|
||||
@@ -335,7 +335,7 @@ To implement a queue, we need a data structure that allows adding elements at on
|
||||
|
||||
### 1. Implementation based on a linked list
|
||||
|
||||
As shown in the Figure 5-5 , we can consider the "head node" and "tail node" of a linked list as the "front" and "rear" of the queue, respectively. It is stipulated that nodes can only be added at the rear and removed at the front.
|
||||
As shown in Figure 5-5, we can consider the "head node" and "tail node" of a linked list as the "front" and "rear" of the queue, respectively. It is stipulated that nodes can only be added at the rear and removed at the front.
|
||||
|
||||
=== "LinkedListQueue"
|
||||
{ class="animation-figure" }
|
||||
@@ -1363,7 +1363,7 @@ Deleting the first element in an array has a time complexity of $O(n)$, which wo
|
||||
|
||||
We use a variable `front` to indicate the index of the front element and maintain a variable `size` to record the queue's length. Define `rear = front + size`, which points to the position immediately following the tail element.
|
||||
|
||||
With this design, **the effective interval of elements in the array is `[front, rear - 1]`**. The implementation methods for various operations are shown in the Figure 5-6 .
|
||||
With this design, **the effective interval of elements in the array is `[front, rear - 1]`**. The implementation methods for various operations are shown in Figure 5-6.
|
||||
|
||||
- Enqueue operation: Assign the input element to the `rear` index and increase `size` by 1.
|
||||
- Dequeue operation: Simply increase `front` by 1 and decrease `size` by 1.
|
||||
|
||||
@@ -8,7 +8,7 @@ A "Stack" is a linear data structure that follows the principle of Last-In-First
|
||||
|
||||
We can compare a stack to a pile of plates on a table. To access the bottom plate, one must first remove the plates on top. By replacing the plates with various types of elements (such as integers, characters, objects, etc.), we obtain the data structure known as a stack.
|
||||
|
||||
As shown in the Figure 5-1 , we refer to the top of the pile of elements as the "top of the stack" and the bottom as the "bottom of the stack." The operation of adding elements to the top of the stack is called "push," and the operation of removing the top element is called "pop."
|
||||
As shown in Figure 5-1, we refer to the top of the pile of elements as the "top of the stack" and the bottom as the "bottom of the stack." The operation of adding elements to the top of the stack is called "push," and the operation of removing the top element is called "pop."
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -16,7 +16,7 @@ As shown in the Figure 5-1 , we refer to the top of the pile of elements as the
|
||||
|
||||
## 5.1.1 Common operations on stack
|
||||
|
||||
The common operations on a stack are shown in the Table 5-1 . The specific method names depend on the programming language used. Here, we use `push()`, `pop()`, and `peek()` as examples.
|
||||
The common operations on a stack are shown in Table 5-1. The specific method names depend on the programming language used. Here, we use `push()`, `pop()`, and `peek()` as examples.
|
||||
|
||||
<p align="center"> Table 5-1 Efficiency of stack operations </p>
|
||||
|
||||
@@ -333,7 +333,7 @@ A stack follows the principle of Last-In-First-Out, which means we can only add
|
||||
|
||||
When implementing a stack using a linked list, we can consider the head node of the list as the top of the stack and the tail node as the bottom of the stack.
|
||||
|
||||
As shown in the Figure 5-2 , for the push operation, we simply insert elements at the head of the linked list. This method of node insertion is known as "head insertion." For the pop operation, we just need to remove the head node from the list.
|
||||
As shown in Figure 5-2, for the push operation, we simply insert elements at the head of the linked list. This method of node insertion is known as "head insertion." For the pop operation, we just need to remove the head node from the list.
|
||||
|
||||
=== "LinkedListStack"
|
||||
{ class="animation-figure" }
|
||||
@@ -1206,7 +1206,7 @@ Below is an example code for implementing a stack based on a linked list:
|
||||
|
||||
### 2. Implementation based on an array
|
||||
|
||||
When implementing a stack using an array, we can consider the end of the array as the top of the stack. As shown in the Figure 5-3 , push and pop operations correspond to adding and removing elements at the end of the array, respectively, both with a time complexity of $O(1)$.
|
||||
When implementing a stack using an array, we can consider the end of the array as the top of the stack. As shown in Figure 5-3, push and pop operations correspond to adding and removing elements at the end of the array, respectively, both with a time complexity of $O(1)$.
|
||||
|
||||
=== "ArrayStack"
|
||||
{ class="animation-figure" }
|
||||
|
||||
Reference in New Issue
Block a user