From 620abe42f3a8c4bab9072828409fc4c27558a869 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Sun, 11 Oct 2026 00:58:16 +0800 Subject: [PATCH] fix: sync array, deque, and printing fixes across translations (#1973) Synchronize the fixes from #1936, #1824, and #1961 across translated editions and normalize C# and Dart formatting. Move Java CI to Ubuntu 24.04 so its Java 11/17 checks can run. --- .github/workflows/java.yml | 4 ++- .../linkedlist_deque.cs | 3 +- .../linkedlist_deque.dart | 6 ++-- .../linkedlist_deque.c | 21 +++++++----- en/codes/c/utils/print_util.h | 4 +-- .../linkedlist_deque.cpp | 21 +++++++----- .../linkedlist_deque.cs | 24 +++++++------ .../linkedlist_deque.dart | 24 +++++++------ .../linkedlist_deque.java | 20 ++++++----- .../linkedlist_deque.js | 24 +++++++++---- .../linkedlist_deque.kt | 20 ++++++----- .../linkedlist_deque.py | 17 ++++++---- .../linkedlist_deque.rb | 19 ++++++----- .../linkedlist_deque.swift | 20 ++++++----- .../linkedlist_deque.ts | 34 ++++++++++++------- en/docs/chapter_array_and_linkedlist/array.md | 4 +-- .../linkedlist_deque.c | 21 +++++++----- ja/codes/c/utils/print_util.h | 4 +-- .../linkedlist_deque.cpp | 21 +++++++----- .../linkedlist_deque.cs | 24 +++++++------ .../linkedlist_deque.dart | 24 +++++++------ .../linkedlist_deque.java | 20 ++++++----- .../linkedlist_deque.js | 24 +++++++++---- .../linkedlist_deque.kt | 20 ++++++----- .../linkedlist_deque.py | 17 ++++++---- .../linkedlist_deque.rb | 19 ++++++----- .../linkedlist_deque.swift | 20 ++++++----- .../linkedlist_deque.ts | 34 ++++++++++++------- .../linkedlist_deque.zig | 20 ++++++----- ja/docs/chapter_array_and_linkedlist/array.md | 4 +-- .../linkedlist_deque.c | 21 +++++++----- ru/codes/c/utils/print_util.h | 4 +-- .../linkedlist_deque.cpp | 21 +++++++----- .../linkedlist_deque.cs | 24 +++++++------ .../linkedlist_deque.dart | 24 +++++++------ .../linkedlist_deque.java | 20 ++++++----- .../linkedlist_deque.js | 24 +++++++++---- .../linkedlist_deque.kt | 20 ++++++----- .../linkedlist_deque.py | 17 ++++++---- .../linkedlist_deque.rb | 19 ++++++----- .../linkedlist_deque.swift | 20 ++++++----- .../linkedlist_deque.ts | 34 ++++++++++++------- .../linkedlist_deque.zig | 20 ++++++----- ru/docs/chapter_array_and_linkedlist/array.md | 4 +-- .../linkedlist_deque.c | 21 +++++++----- zh-hant/codes/c/utils/print_util.h | 4 +-- .../linkedlist_deque.cpp | 21 +++++++----- .../linkedlist_deque.cs | 24 +++++++------ .../linkedlist_deque.dart | 24 +++++++------ .../linkedlist_deque.java | 20 ++++++----- .../linkedlist_deque.js | 24 +++++++++---- .../linkedlist_deque.kt | 20 ++++++----- .../linkedlist_deque.py | 17 ++++++---- .../linkedlist_deque.rb | 19 ++++++----- .../linkedlist_deque.swift | 20 ++++++----- .../linkedlist_deque.ts | 34 ++++++++++++------- .../linkedlist_deque.zig | 20 ++++++----- .../chapter_array_and_linkedlist/array.md | 4 +-- 58 files changed, 624 insertions(+), 457 deletions(-) diff --git a/.github/workflows/java.yml b/.github/workflows/java.yml index 7e2bbfc5d..9e86df661 100644 --- a/.github/workflows/java.yml +++ b/.github/workflows/java.yml @@ -7,18 +7,20 @@ on: push: branches: ["main"] paths: + - ".github/workflows/java.yml" - "codes/java/**/*.java" - "en/codes/java/**/*.java" pull_request: branches: ["main"] paths: + - ".github/workflows/java.yml" - "codes/java/**/*.java" - "en/codes/java/**/*.java" workflow_dispatch: jobs: build: - runs-on: ubuntu-20.04 + runs-on: ubuntu-24.04 strategy: matrix: java: ["11", "17"] diff --git a/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs b/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs index a2809ccd7..e87513eb4 100644 --- a/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs +++ b/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs @@ -75,8 +75,7 @@ public class LinkedListDeque { throw new Exception(); int? val; // 队列长度为 1 时, 头尾指向同一节点 - if (queSize == 1) - { + if (queSize == 1) { val = front!.val; front = null; rear = null; diff --git a/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart b/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart index 1dfb0be39..3ecc4abaa 100644 --- a/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart +++ b/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart @@ -75,9 +75,9 @@ class LinkedListDeque { final int val; // 队列长度为 1 时, 头尾指向同一节点 if (_queSize == 1) { - val = _front!.val; - _front = null; - _rear = null; + val = _front!.val; + _front = null; + _rear = null; } // 队首出队操作 else if (isFront) { diff --git a/en/codes/c/chapter_stack_and_queue/linkedlist_deque.c b/en/codes/c/chapter_stack_and_queue/linkedlist_deque.c index fe84a8974..98e32b4d4 100644 --- a/en/codes/c/chapter_stack_and_queue/linkedlist_deque.c +++ b/en/codes/c/chapter_stack_and_queue/linkedlist_deque.c @@ -115,14 +115,19 @@ int pop(LinkedListDeque *deque, bool isFront) { if (empty(deque)) return -1; int val; + // When the deque has one element, front and rear point to the same node + if (deque->queSize == 1) { + val = deque->front->val; + delDoublyListNode(deque->front); // Free the shared node once + deque->front = NULL; + deque->rear = NULL; + } // Temporarily store head node value - if (isFront) { + else if (isFront) { val = peekFirst(deque); // Delete head node DoublyListNode *fNext = deque->front->next; - if (fNext) { - fNext->prev = NULL; - deque->front->next = NULL; - } + fNext->prev = NULL; + deque->front->next = NULL; delDoublyListNode(deque->front); deque->front = fNext; // Update head node } @@ -130,10 +135,8 @@ int pop(LinkedListDeque *deque, bool isFront) { else { val = peekLast(deque); // Delete tail node DoublyListNode *rPrev = deque->rear->prev; - if (rPrev) { - rPrev->next = NULL; - deque->rear->prev = NULL; - } + rPrev->next = NULL; + deque->rear->prev = NULL; delDoublyListNode(deque->rear); deque->rear = rPrev; // Update tail node } diff --git a/en/codes/c/utils/print_util.h b/en/codes/c/utils/print_util.h index f239c3fcb..00289d686 100644 --- a/en/codes/c/utils/print_util.h +++ b/en/codes/c/utils/print_util.h @@ -64,8 +64,8 @@ typedef struct Trunk { Trunk *newTrunk(Trunk *prev, char *str) { Trunk *trunk = (Trunk *)malloc(sizeof(Trunk)); trunk->prev = prev; - trunk->str = (char *)malloc(sizeof(char) * 10); - strcpy(trunk->str, str); + trunk->str = (char *)malloc(strlen(str) + 1); + snprintf(trunk->str, strlen(str) + 1, "%s", str); return trunk; } diff --git a/en/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp b/en/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp index eb3eb59b1..e0999fd7b 100644 --- a/en/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp +++ b/en/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp @@ -84,15 +84,20 @@ class LinkedListDeque { if (isEmpty()) throw out_of_range("Queue is empty"); int val; + // When the deque has one element, front and rear point to the same node + if (queSize == 1) { + val = front->val; + delete front; // Free the shared node once + front = nullptr; + rear = nullptr; + } // Temporarily store head node value - if (isFront) { + else if (isFront) { val = front->val; // Delete head node // Delete head node DoublyListNode *fNext = front->next; - if (fNext != nullptr) { - fNext->prev = nullptr; - front->next = nullptr; - } + fNext->prev = nullptr; + front->next = nullptr; delete front; front = fNext; // Update head node // Temporarily store tail node value @@ -100,10 +105,8 @@ class LinkedListDeque { val = rear->val; // Delete tail node // Update tail node DoublyListNode *rPrev = rear->prev; - if (rPrev != nullptr) { - rPrev->next = nullptr; - rear->prev = nullptr; - } + rPrev->next = nullptr; + rear->prev = nullptr; delete rear; rear = rPrev; // Update tail node } diff --git a/en/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs b/en/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs index 38515edc1..50fa3554c 100644 --- a/en/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs +++ b/en/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs @@ -74,26 +74,28 @@ public class LinkedListDeque { if (IsEmpty()) throw new Exception(); int? val; + // When the deque has one element, front and rear point to the same node + if (queSize == 1) { + val = front!.val; + front = null; + rear = null; + } // Temporarily store head node value - if (isFront) { - val = front?.val; // Delete head node + else if (isFront) { + val = front!.val; // Delete head node // Delete head node ListNode? fNext = front?.next; - if (fNext != null) { - fNext.prev = null; - front!.next = null; - } + fNext!.prev = null; + front.next = null; front = fNext; // Update head node } // Temporarily store tail node value else { - val = rear?.val; // Delete tail node + val = rear!.val; // Delete tail node // Update tail node ListNode? rPrev = rear?.prev; - if (rPrev != null) { - rPrev.next = null; - rear!.prev = null; - } + rPrev!.next = null; + rear.prev = null; rear = rPrev; // Update tail node } diff --git a/en/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart b/en/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart index ce5c0ed8c..2f20f1de2 100644 --- a/en/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart +++ b/en/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart @@ -73,25 +73,27 @@ class LinkedListDeque { return null; } final int val; - if (isFront) { - // Temporarily store head node value + // When the deque has one element, front and rear point to the same node + if (_queSize == 1) { + val = _front!.val; + _front = null; + _rear = null; + } + // Temporarily store head node value + else if (isFront) { val = _front!.val; // Delete head node // Delete head node ListNode? fNext = _front!.next; - if (fNext != null) { - fNext.prev = null; - _front!.next = null; - } + fNext!.prev = null; + _front!.next = null; _front = fNext; // Update head node + // Temporarily store tail node value } else { - // Temporarily store tail node value val = _rear!.val; // Delete tail node // Update tail node ListNode? rPrev = _rear!.prev; - if (rPrev != null) { - rPrev.next = null; - _rear!.prev = null; - } + rPrev!.next = null; + _rear!.prev = null; _rear = rPrev; // Update tail node } _queSize--; // Update queue length diff --git a/en/codes/java/chapter_stack_and_queue/linkedlist_deque.java b/en/codes/java/chapter_stack_and_queue/linkedlist_deque.java index b1bfe074a..a2eed7b25 100644 --- a/en/codes/java/chapter_stack_and_queue/linkedlist_deque.java +++ b/en/codes/java/chapter_stack_and_queue/linkedlist_deque.java @@ -76,25 +76,27 @@ class LinkedListDeque { if (isEmpty()) throw new IndexOutOfBoundsException(); int val; + // When the deque has one element, front and rear point to the same node + if (queSize == 1) { + val = front.val; + front = null; + rear = null; + } // Temporarily store head node value - if (isFront) { + else if (isFront) { val = front.val; // Delete head node // Delete head node ListNode fNext = front.next; - if (fNext != null) { - fNext.prev = null; - front.next = null; - } + fNext.prev = null; + front.next = null; front = fNext; // Update head node // Temporarily store tail node value } else { val = rear.val; // Delete tail node // Update tail node ListNode rPrev = rear.prev; - if (rPrev != null) { - rPrev.next = null; - rear.prev = null; - } + rPrev.next = null; + rear.prev = null; rear = rPrev; // Update tail node } queSize--; // Update queue length diff --git a/en/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js b/en/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js index 717b3fa32..03b51022d 100644 --- a/en/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js +++ b/en/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js @@ -67,13 +67,18 @@ class LinkedListDeque { return null; } const value = this.#rear.val; // Store tail node value + // When the deque has one element, front and rear point to the same node + if (this.#queSize === 1) { + this.#front = null; + this.#rear = null; + } // Update tail node - let temp = this.#rear.prev; - if (temp !== null) { + else { + let temp = this.#rear.prev; temp.next = null; this.#rear.prev = null; + this.#rear = temp; // Update tail node } - this.#rear = temp; // Update tail node this.#queSize--; return value; } @@ -83,14 +88,19 @@ class LinkedListDeque { if (this.#queSize === 0) { return null; } - const value = this.#front.val; // Store tail node value + const value = this.#front.val; // Store the head node value + // When the deque has one element, front and rear point to the same node + if (this.#queSize === 1) { + this.#front = null; + this.#rear = null; + } // Delete head node - let temp = this.#front.next; - if (temp !== null) { + else { + let temp = this.#front.next; temp.prev = null; this.#front.next = null; + this.#front = temp; // Update head node } - this.#front = temp; // Update head node this.#queSize--; return value; } diff --git a/en/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt b/en/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt index 75b555c85..2ffa825d8 100644 --- a/en/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt +++ b/en/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt @@ -67,25 +67,27 @@ class LinkedListDeque { if (isEmpty()) throw IndexOutOfBoundsException() val _val: Int + // When the deque has one element, front and rear point to the same node + if (queSize == 1) { + _val = front!!._val + front = null + rear = null + } // Temporarily store head node value - if (isFront) { + else if (isFront) { _val = front!!._val // Delete head node // Delete head node val fNext = front!!.next - if (fNext != null) { - fNext.prev = null - front!!.next = null - } + fNext!!.prev = null + front!!.next = null front = fNext // Update head node // Temporarily store tail node value } else { _val = rear!!._val // Delete tail node // Update tail node val rPrev = rear!!.prev - if (rPrev != null) { - rPrev.next = null - rear!!.prev = null - } + rPrev!!.next = null + rear!!.prev = null rear = rPrev // Update tail node } queSize-- // Update queue length diff --git a/en/codes/python/chapter_stack_and_queue/linkedlist_deque.py b/en/codes/python/chapter_stack_and_queue/linkedlist_deque.py index 488725b76..081669661 100644 --- a/en/codes/python/chapter_stack_and_queue/linkedlist_deque.py +++ b/en/codes/python/chapter_stack_and_queue/linkedlist_deque.py @@ -64,23 +64,26 @@ class LinkedListDeque: """Dequeue operation""" if self.is_empty(): raise IndexError("Double-ended queue is empty") + # When the deque has one element, front and rear point to the same node + if self._size == 1: + val: int = self._front.val + self._front = None + self._rear = None # Front of the queue dequeue operation - if is_front: + elif is_front: val: int = self._front.val # Temporarily store head node value # Delete head node fnext: ListNode | None = self._front.next - if fnext is not None: - fnext.prev = None - self._front.next = None + fnext.prev = None + self._front.next = None self._front = fnext # Update head node # Rear of the queue dequeue operation else: val: int = self._rear.val # Temporarily store tail node value # Delete tail node rprev: ListNode | None = self._rear.prev - if rprev is not None: - rprev.next = None - self._rear.prev = None + rprev.next = None + self._rear.prev = None self._rear = rprev # Update tail node self._size -= 1 # Update queue length return val diff --git a/en/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb b/en/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb index c397fa976..f6b499d34 100644 --- a/en/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb +++ b/en/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb @@ -69,25 +69,26 @@ class LinkedListDeque def pop(is_front) raise IndexError, 'Deque is empty' if is_empty? + # When the deque has one element, front and rear point to the same node + if @size == 1 + val = @front.val + @front = nil + @rear = nil # Temporarily store head node value - if is_front + elsif is_front val = @front.val # Delete head node # Delete head node fnext = @front.next - unless fnext.nil? - fnext.prev = nil - @front.next = nil - end + fnext.prev = nil + @front.next = nil @front = fnext # Update head node # Temporarily store tail node value else val = @rear.val # Delete tail node # Update tail node rprev = @rear.prev - unless rprev.nil? - rprev.next = nil - @rear.prev = nil - end + rprev.next = nil + @rear.prev = nil @rear = rprev # Update tail node end @size -= 1 # Update queue length diff --git a/en/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift b/en/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift index 5619b84c5..50b100131 100644 --- a/en/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift +++ b/en/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift @@ -76,15 +76,19 @@ class LinkedListDeque { fatalError("Deque is empty") } let val: Int + // When the deque has one element, front and rear point to the same node + if _size == 1 { + val = front!.val + front = nil + rear = nil + } // Temporarily store head node value - if isFront { + else if isFront { val = front!.val // Delete head node // Delete head node let fNext = front?.next - if fNext != nil { - fNext?.prev = nil - front?.next = nil - } + fNext?.prev = nil + front?.next = nil front = fNext // Update head node } // Temporarily store tail node value @@ -92,10 +96,8 @@ class LinkedListDeque { val = rear!.val // Delete tail node // Update tail node let rPrev = rear?.prev - if rPrev != nil { - rPrev?.next = nil - rear?.prev = nil - } + rPrev?.next = nil + rear?.prev = nil rear = rPrev // Update tail node } _size -= 1 // Update queue length diff --git a/en/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts b/en/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts index b8fa239e3..88680fad8 100644 --- a/en/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts +++ b/en/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts @@ -66,14 +66,19 @@ class LinkedListDeque { if (this.queSize === 0) { return null; } - const value: number = this.rear.val; // Store tail node value + const value: number = this.rear!.val; // Store tail node value + // When the deque has one element, front and rear point to the same node + if (this.queSize === 1) { + this.front = null; + this.rear = null; + } // Update tail node - let temp: ListNode = this.rear.prev; - if (temp !== null) { - temp.next = null; - this.rear.prev = null; + else { + let temp: ListNode = this.rear!.prev; + temp!.next = null; + this.rear!.prev = null; + this.rear = temp; // Update tail node } - this.rear = temp; // Update tail node this.queSize--; return value; } @@ -83,14 +88,19 @@ class LinkedListDeque { if (this.queSize === 0) { return null; } - const value: number = this.front.val; // Store tail node value + const value: number = this.front.val; // Store the head node value + // When the deque has one element, front and rear point to the same node + if (this.queSize === 1) { + this.front = null; + this.rear = null; + } // Delete head node - let temp: ListNode = this.front.next; - if (temp !== null) { - temp.prev = null; - this.front.next = null; + else { + let temp: ListNode = this.front!.next; + temp!.prev = null; + this.front!.next = null; + this.front = temp; // Update head node } - this.front = temp; // Update head node this.queSize--; return value; } diff --git a/en/docs/chapter_array_and_linkedlist/array.md b/en/docs/chapter_array_and_linkedlist/array.md index eed5bd3f8..6e27f36a9 100755 --- a/en/docs/chapter_array_and_linkedlist/array.md +++ b/en/docs/chapter_array_and_linkedlist/array.md @@ -23,10 +23,10 @@ We can choose between two array initialization methods based on our needs: with ```cpp title="array.cpp" /* Initialize array */ // Stored on stack - int arr[5]; + int arr[5] = {}; int nums[5] = { 1, 3, 2, 5, 4 }; // Stored on heap (requires manual memory release) - int* arr1 = new int[5]; + int* arr1 = new int[5] {}; int* nums1 = new int[5] { 1, 3, 2, 5, 4 }; ``` diff --git a/ja/codes/c/chapter_stack_and_queue/linkedlist_deque.c b/ja/codes/c/chapter_stack_and_queue/linkedlist_deque.c index 55aaa152b..a5901491a 100644 --- a/ja/codes/c/chapter_stack_and_queue/linkedlist_deque.c +++ b/ja/codes/c/chapter_stack_and_queue/linkedlist_deque.c @@ -115,14 +115,19 @@ int pop(LinkedListDeque *deque, bool isFront) { if (empty(deque)) return -1; int val; + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (deque->queSize == 1) { + val = deque->front->val; + delDoublyListNode(deque->front); // 同じノードを一度だけ解放する + deque->front = NULL; + deque->rear = NULL; + } // キュー先頭からの取り出し - if (isFront) { + else if (isFront) { val = peekFirst(deque); // 先頭ノードの値を一時保存 DoublyListNode *fNext = deque->front->next; - if (fNext) { - fNext->prev = NULL; - deque->front->next = NULL; - } + fNext->prev = NULL; + deque->front->next = NULL; delDoublyListNode(deque->front); deque->front = fNext; // 先頭ノードを更新する } @@ -130,10 +135,8 @@ int pop(LinkedListDeque *deque, bool isFront) { else { val = peekLast(deque); // 末尾ノードの値を一時保存 DoublyListNode *rPrev = deque->rear->prev; - if (rPrev) { - rPrev->next = NULL; - deque->rear->prev = NULL; - } + rPrev->next = NULL; + deque->rear->prev = NULL; delDoublyListNode(deque->rear); deque->rear = rPrev; // 末尾ノードを更新する } diff --git a/ja/codes/c/utils/print_util.h b/ja/codes/c/utils/print_util.h index b0be4c263..24f52636b 100644 --- a/ja/codes/c/utils/print_util.h +++ b/ja/codes/c/utils/print_util.h @@ -64,8 +64,8 @@ typedef struct Trunk { Trunk *newTrunk(Trunk *prev, char *str) { Trunk *trunk = (Trunk *)malloc(sizeof(Trunk)); trunk->prev = prev; - trunk->str = (char *)malloc(sizeof(char) * 10); - strcpy(trunk->str, str); + trunk->str = (char *)malloc(strlen(str) + 1); + snprintf(trunk->str, strlen(str) + 1, "%s", str); return trunk; } diff --git a/ja/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp b/ja/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp index 8a9d62971..b4ad3f205 100644 --- a/ja/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp +++ b/ja/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp @@ -84,15 +84,20 @@ class LinkedListDeque { if (isEmpty()) throw out_of_range("キューが空です"); int val; + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (queSize == 1) { + val = front->val; + delete front; // 同じノードを一度だけ解放する + front = nullptr; + rear = nullptr; + } // キュー先頭からの取り出し - if (isFront) { + else if (isFront) { val = front->val; // 先頭ノードの値を一時保存 // 先頭ノードを削除 DoublyListNode *fNext = front->next; - if (fNext != nullptr) { - fNext->prev = nullptr; - front->next = nullptr; - } + fNext->prev = nullptr; + front->next = nullptr; delete front; front = fNext; // 先頭ノードを更新する // キュー末尾からの取り出し @@ -100,10 +105,8 @@ class LinkedListDeque { val = rear->val; // 末尾ノードの値を一時保存 // 末尾ノードを削除 DoublyListNode *rPrev = rear->prev; - if (rPrev != nullptr) { - rPrev->next = nullptr; - rear->prev = nullptr; - } + rPrev->next = nullptr; + rear->prev = nullptr; delete rear; rear = rPrev; // 末尾ノードを更新する } diff --git a/ja/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs b/ja/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs index 628ae85a3..224262454 100644 --- a/ja/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs +++ b/ja/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs @@ -74,26 +74,28 @@ public class LinkedListDeque { if (IsEmpty()) throw new Exception(); int? val; + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (queSize == 1) { + val = front!.val; + front = null; + rear = null; + } // キュー先頭からの取り出し - if (isFront) { - val = front?.val; // 先頭ノードの値を一時保存 + else if (isFront) { + val = front!.val; // 先頭ノードの値を一時保存 // 先頭ノードを削除 ListNode? fNext = front?.next; - if (fNext != null) { - fNext.prev = null; - front!.next = null; - } + fNext!.prev = null; + front.next = null; front = fNext; // 先頭ノードを更新する } // キュー末尾からの取り出し else { - val = rear?.val; // 末尾ノードの値を一時保存 + val = rear!.val; // 末尾ノードの値を一時保存 // 末尾ノードを削除 ListNode? rPrev = rear?.prev; - if (rPrev != null) { - rPrev.next = null; - rear!.prev = null; - } + rPrev!.next = null; + rear.prev = null; rear = rPrev; // 末尾ノードを更新する } diff --git a/ja/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart b/ja/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart index bd2c911bb..d88ca1f5b 100644 --- a/ja/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart +++ b/ja/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart @@ -73,25 +73,27 @@ class LinkedListDeque { return null; } final int val; - if (isFront) { - // キュー先頭からの取り出し + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (_queSize == 1) { + val = _front!.val; + _front = null; + _rear = null; + } + // キュー先頭からの取り出し + else if (isFront) { val = _front!.val; // 先頭ノードの値を一時保存 // 先頭ノードを削除 ListNode? fNext = _front!.next; - if (fNext != null) { - fNext.prev = null; - _front!.next = null; - } + fNext!.prev = null; + _front!.next = null; _front = fNext; // 先頭ノードを更新する + // キュー末尾からの取り出し } else { - // キュー末尾からの取り出し val = _rear!.val; // 末尾ノードの値を一時保存 // 末尾ノードを削除 ListNode? rPrev = _rear!.prev; - if (rPrev != null) { - rPrev.next = null; - _rear!.prev = null; - } + rPrev!.next = null; + _rear!.prev = null; _rear = rPrev; // 末尾ノードを更新する } _queSize--; // キューの長さを更新 diff --git a/ja/codes/java/chapter_stack_and_queue/linkedlist_deque.java b/ja/codes/java/chapter_stack_and_queue/linkedlist_deque.java index 6f2f26dfe..5d2e8898a 100644 --- a/ja/codes/java/chapter_stack_and_queue/linkedlist_deque.java +++ b/ja/codes/java/chapter_stack_and_queue/linkedlist_deque.java @@ -76,25 +76,27 @@ class LinkedListDeque { if (isEmpty()) throw new IndexOutOfBoundsException(); int val; + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (queSize == 1) { + val = front.val; + front = null; + rear = null; + } // キュー先頭からの取り出し - if (isFront) { + else if (isFront) { val = front.val; // 先頭ノードの値を一時保存 // 先頭ノードを削除 ListNode fNext = front.next; - if (fNext != null) { - fNext.prev = null; - front.next = null; - } + fNext.prev = null; + front.next = null; front = fNext; // 先頭ノードを更新する // キュー末尾からの取り出し } else { val = rear.val; // 末尾ノードの値を一時保存 // 末尾ノードを削除 ListNode rPrev = rear.prev; - if (rPrev != null) { - rPrev.next = null; - rear.prev = null; - } + rPrev.next = null; + rear.prev = null; rear = rPrev; // 末尾ノードを更新する } queSize--; // キューの長さを更新 diff --git a/ja/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js b/ja/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js index bf76c8be0..49d1611d8 100644 --- a/ja/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js +++ b/ja/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js @@ -67,13 +67,18 @@ class LinkedListDeque { return null; } const value = this.#rear.val; // 末尾ノードの値を保存する + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (this.#queSize === 1) { + this.#front = null; + this.#rear = null; + } // 末尾ノードを削除 - let temp = this.#rear.prev; - if (temp !== null) { + else { + let temp = this.#rear.prev; temp.next = null; this.#rear.prev = null; + this.#rear = temp; // 末尾ノードを更新する } - this.#rear = temp; // 末尾ノードを更新する this.#queSize--; return value; } @@ -83,14 +88,19 @@ class LinkedListDeque { if (this.#queSize === 0) { return null; } - const value = this.#front.val; // 末尾ノードの値を保存する + const value = this.#front.val; // 先頭ノードの値を保存 + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (this.#queSize === 1) { + this.#front = null; + this.#rear = null; + } // 先頭ノードを削除 - let temp = this.#front.next; - if (temp !== null) { + else { + let temp = this.#front.next; temp.prev = null; this.#front.next = null; + this.#front = temp; // 先頭ノードを更新する } - this.#front = temp; // 先頭ノードを更新する this.#queSize--; return value; } diff --git a/ja/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt b/ja/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt index 4aba13f7b..14dfb6463 100644 --- a/ja/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt +++ b/ja/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt @@ -67,25 +67,27 @@ class LinkedListDeque { if (isEmpty()) throw IndexOutOfBoundsException() val _val: Int + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (queSize == 1) { + _val = front!!._val + front = null + rear = null + } // キュー先頭からの取り出し - if (isFront) { + else if (isFront) { _val = front!!._val // 先頭ノードの値を一時保存 // 先頭ノードを削除 val fNext = front!!.next - if (fNext != null) { - fNext.prev = null - front!!.next = null - } + fNext!!.prev = null + front!!.next = null front = fNext // 先頭ノードを更新する // キュー末尾からの取り出し } else { _val = rear!!._val // 末尾ノードの値を一時保存 // 末尾ノードを削除 val rPrev = rear!!.prev - if (rPrev != null) { - rPrev.next = null - rear!!.prev = null - } + rPrev!!.next = null + rear!!.prev = null rear = rPrev // 末尾ノードを更新する } queSize-- // キューの長さを更新 diff --git a/ja/codes/python/chapter_stack_and_queue/linkedlist_deque.py b/ja/codes/python/chapter_stack_and_queue/linkedlist_deque.py index 1eea9c44d..81816e56f 100644 --- a/ja/codes/python/chapter_stack_and_queue/linkedlist_deque.py +++ b/ja/codes/python/chapter_stack_and_queue/linkedlist_deque.py @@ -64,23 +64,26 @@ class LinkedListDeque: """デキュー操作""" if self.is_empty(): raise IndexError("両端キューが空です") + # 要素が1つの場合、先頭と末尾は同じノードを指す + if self._size == 1: + val: int = self._front.val + self._front = None + self._rear = None # キュー先頭からの取り出し - if is_front: + elif is_front: val: int = self._front.val # 先頭ノードの値を一時保存 # 先頭ノードを削除 fnext: ListNode | None = self._front.next - if fnext is not None: - fnext.prev = None - self._front.next = None + fnext.prev = None + self._front.next = None self._front = fnext # 先頭ノードを更新する # キュー末尾からの取り出し else: val: int = self._rear.val # 末尾ノードの値を一時保存 # 末尾ノードを削除 rprev: ListNode | None = self._rear.prev - if rprev is not None: - rprev.next = None - self._rear.prev = None + rprev.next = None + self._rear.prev = None self._rear = rprev # 末尾ノードを更新する self._size -= 1 # キューの長さを更新 return val diff --git a/ja/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb b/ja/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb index 11519f74b..c93a0e64b 100644 --- a/ja/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb +++ b/ja/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb @@ -69,25 +69,26 @@ class LinkedListDeque def pop(is_front) raise IndexError, '両端キューは空です' if is_empty? + # 要素が1つの場合、先頭と末尾は同じノードを指す + if @size == 1 + val = @front.val + @front = nil + @rear = nil # キュー先頭からの取り出し - if is_front + elsif is_front val = @front.val # 先頭ノードの値を一時保存 # 先頭ノードを削除 fnext = @front.next - unless fnext.nil? - fnext.prev = nil - @front.next = nil - end + fnext.prev = nil + @front.next = nil @front = fnext # 先頭ノードを更新する # キュー末尾からの取り出し else val = @rear.val # 末尾ノードの値を一時保存 # 末尾ノードを削除 rprev = @rear.prev - unless rprev.nil? - rprev.next = nil - @rear.prev = nil - end + rprev.next = nil + @rear.prev = nil @rear = rprev # 末尾ノードを更新する end @size -= 1 # キューの長さを更新 diff --git a/ja/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift b/ja/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift index ad5e46389..c69db6ee0 100644 --- a/ja/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift +++ b/ja/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift @@ -76,15 +76,19 @@ class LinkedListDeque { fatalError("両端キューが空です") } let val: Int + // 要素が1つの場合、先頭と末尾は同じノードを指す + if _size == 1 { + val = front!.val + front = nil + rear = nil + } // キュー先頭からの取り出し - if isFront { + else if isFront { val = front!.val // 先頭ノードの値を一時保存 // 先頭ノードを削除 let fNext = front?.next - if fNext != nil { - fNext?.prev = nil - front?.next = nil - } + fNext?.prev = nil + front?.next = nil front = fNext // 先頭ノードを更新する } // キュー末尾からの取り出し @@ -92,10 +96,8 @@ class LinkedListDeque { val = rear!.val // 末尾ノードの値を一時保存 // 末尾ノードを削除 let rPrev = rear?.prev - if rPrev != nil { - rPrev?.next = nil - rear?.prev = nil - } + rPrev?.next = nil + rear?.prev = nil rear = rPrev // 末尾ノードを更新する } _size -= 1 // キューの長さを更新 diff --git a/ja/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts b/ja/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts index f988a4230..4bf5539ba 100644 --- a/ja/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts +++ b/ja/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts @@ -66,14 +66,19 @@ class LinkedListDeque { if (this.queSize === 0) { return null; } - const value: number = this.rear.val; // 末尾ノードの値を保存する + const value: number = this.rear!.val; // 末尾ノードの値を保存する + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (this.queSize === 1) { + this.front = null; + this.rear = null; + } // 末尾ノードを削除 - let temp: ListNode = this.rear.prev; - if (temp !== null) { - temp.next = null; - this.rear.prev = null; + else { + let temp: ListNode = this.rear!.prev; + temp!.next = null; + this.rear!.prev = null; + this.rear = temp; // 末尾ノードを更新する } - this.rear = temp; // 末尾ノードを更新する this.queSize--; return value; } @@ -83,14 +88,19 @@ class LinkedListDeque { if (this.queSize === 0) { return null; } - const value: number = this.front.val; // 末尾ノードの値を保存する + const value: number = this.front.val; // 先頭ノードの値を保存 + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (this.queSize === 1) { + this.front = null; + this.rear = null; + } // 先頭ノードを削除 - let temp: ListNode = this.front.next; - if (temp !== null) { - temp.prev = null; - this.front.next = null; + else { + let temp: ListNode = this.front!.next; + temp!.prev = null; + this.front!.next = null; + this.front = temp; // 先頭ノードを更新する } - this.front = temp; // 先頭ノードを更新する this.queSize--; return value; } diff --git a/ja/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig b/ja/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig index bd9950394..8082a3b12 100644 --- a/ja/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig +++ b/ja/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig @@ -99,25 +99,27 @@ pub fn LinkedListDeque(comptime T: type) type { pub fn pop(self: *Self, is_front: bool) T { if (self.isEmpty()) @panic("両端キューが空です"); var val: T = undefined; + // 要素が1つの場合、先頭と末尾は同じノードを指す + if (self.que_size == 1) { + val = self.front.?.val; + self.front = null; + self.rear = null; + } // キュー先頭からの取り出し - if (is_front) { + else if (is_front) { val = self.front.?.val; // 先頭ノードの値を一時保存 // 先頭ノードを削除 var fNext = self.front.?.next; - if (fNext != null) { - fNext.?.prev = null; - self.front.?.next = null; - } + fNext.?.prev = null; + self.front.?.next = null; self.front = fNext; // 先頭ノードを更新する // キュー末尾からの取り出し } else { val = self.rear.?.val; // 末尾ノードの値を一時保存 // 末尾ノードを削除 var rPrev = self.rear.?.prev; - if (rPrev != null) { - rPrev.?.next = null; - self.rear.?.prev = null; - } + rPrev.?.next = null; + self.rear.?.prev = null; self.rear = rPrev; // 末尾ノードを更新する } self.que_size -= 1; // キューの長さを更新 diff --git a/ja/docs/chapter_array_and_linkedlist/array.md b/ja/docs/chapter_array_and_linkedlist/array.md index 60842af8a..a365da0f0 100644 --- a/ja/docs/chapter_array_and_linkedlist/array.md +++ b/ja/docs/chapter_array_and_linkedlist/array.md @@ -23,10 +23,10 @@ ```cpp title="array.cpp" /* 配列を初期化する */ // スタック上に格納 - int arr[5]; + int arr[5] = {}; int nums[5] = { 1, 3, 2, 5, 4 }; // ヒープ上に格納(手動で領域を解放する必要がある) - int* arr1 = new int[5]; + int* arr1 = new int[5] {}; int* nums1 = new int[5] { 1, 3, 2, 5, 4 }; ``` diff --git a/ru/codes/c/chapter_stack_and_queue/linkedlist_deque.c b/ru/codes/c/chapter_stack_and_queue/linkedlist_deque.c index eae7185a1..9d4053451 100644 --- a/ru/codes/c/chapter_stack_and_queue/linkedlist_deque.c +++ b/ru/codes/c/chapter_stack_and_queue/linkedlist_deque.c @@ -115,14 +115,19 @@ int pop(LinkedListDeque *deque, bool isFront) { if (empty(deque)) return -1; int val; + // Если в очереди один элемент, начало и конец указывают на один узел + if (deque->queSize == 1) { + val = deque->front->val; + delDoublyListNode(deque->front); // Освобождаем общий узел один раз + deque->front = NULL; + deque->rear = NULL; + } // Операция извлечения из головы очереди - if (isFront) { + else if (isFront) { val = peekFirst(deque); // Временно сохранить значение головного узла DoublyListNode *fNext = deque->front->next; - if (fNext) { - fNext->prev = NULL; - deque->front->next = NULL; - } + fNext->prev = NULL; + deque->front->next = NULL; delDoublyListNode(deque->front); deque->front = fNext; // Обновить головной узел } @@ -130,10 +135,8 @@ int pop(LinkedListDeque *deque, bool isFront) { else { val = peekLast(deque); // Временно сохранить значение хвостового узла DoublyListNode *rPrev = deque->rear->prev; - if (rPrev) { - rPrev->next = NULL; - deque->rear->prev = NULL; - } + rPrev->next = NULL; + deque->rear->prev = NULL; delDoublyListNode(deque->rear); deque->rear = rPrev; // Обновить хвостовой узел } diff --git a/ru/codes/c/utils/print_util.h b/ru/codes/c/utils/print_util.h index fd963dd66..14f6983f5 100644 --- a/ru/codes/c/utils/print_util.h +++ b/ru/codes/c/utils/print_util.h @@ -64,8 +64,8 @@ typedef struct Trunk { Trunk *newTrunk(Trunk *prev, char *str) { Trunk *trunk = (Trunk *)malloc(sizeof(Trunk)); trunk->prev = prev; - trunk->str = (char *)malloc(sizeof(char) * 10); - strcpy(trunk->str, str); + trunk->str = (char *)malloc(strlen(str) + 1); + snprintf(trunk->str, strlen(str) + 1, "%s", str); return trunk; } diff --git a/ru/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp b/ru/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp index 91361058e..a7478d828 100644 --- a/ru/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp +++ b/ru/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp @@ -84,15 +84,20 @@ class LinkedListDeque { if (isEmpty()) throw out_of_range("очередь пуста"); int val; + // Если в очереди один элемент, начало и конец указывают на один узел + if (queSize == 1) { + val = front->val; + delete front; // Освобождаем общий узел один раз + front = nullptr; + rear = nullptr; + } // Операция извлечения из головы очереди - if (isFront) { + else if (isFront) { val = front->val; // Временно сохранить значение головного узла // Удалить головной узел DoublyListNode *fNext = front->next; - if (fNext != nullptr) { - fNext->prev = nullptr; - front->next = nullptr; - } + fNext->prev = nullptr; + front->next = nullptr; delete front; front = fNext; // Обновить головной узел // Операция извлечения из хвоста очереди @@ -100,10 +105,8 @@ class LinkedListDeque { val = rear->val; // Временно сохранить значение хвостового узла // Удалить хвостовой узел DoublyListNode *rPrev = rear->prev; - if (rPrev != nullptr) { - rPrev->next = nullptr; - rear->prev = nullptr; - } + rPrev->next = nullptr; + rear->prev = nullptr; delete rear; rear = rPrev; // Обновить хвостовой узел } diff --git a/ru/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs b/ru/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs index 82e35cb17..6322abad2 100644 --- a/ru/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs +++ b/ru/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs @@ -74,26 +74,28 @@ public class LinkedListDeque { if (IsEmpty()) throw new Exception(); int? val; + // Если в очереди один элемент, начало и конец указывают на один узел + if (queSize == 1) { + val = front!.val; + front = null; + rear = null; + } // Операция извлечения из головы очереди - if (isFront) { - val = front?.val; // Временно сохранить значение головного узла + else if (isFront) { + val = front!.val; // Временно сохранить значение головного узла // Удалить головной узел ListNode? fNext = front?.next; - if (fNext != null) { - fNext.prev = null; - front!.next = null; - } + fNext!.prev = null; + front.next = null; front = fNext; // Обновить головной узел } // Операция извлечения из хвоста очереди else { - val = rear?.val; // Временно сохранить значение хвостового узла + val = rear!.val; // Временно сохранить значение хвостового узла // Удалить хвостовой узел ListNode? rPrev = rear?.prev; - if (rPrev != null) { - rPrev.next = null; - rear!.prev = null; - } + rPrev!.next = null; + rear.prev = null; rear = rPrev; // Обновить хвостовой узел } diff --git a/ru/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart b/ru/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart index 43bafda60..23397e158 100644 --- a/ru/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart +++ b/ru/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart @@ -73,25 +73,27 @@ class LinkedListDeque { return null; } final int val; - if (isFront) { - // Операция извлечения из головы очереди + // Если в очереди один элемент, начало и конец указывают на один узел + if (_queSize == 1) { + val = _front!.val; + _front = null; + _rear = null; + } + // Операция извлечения из головы очереди + else if (isFront) { val = _front!.val; // Временно сохранить значение головного узла // Удалить головной узел ListNode? fNext = _front!.next; - if (fNext != null) { - fNext.prev = null; - _front!.next = null; - } + fNext!.prev = null; + _front!.next = null; _front = fNext; // Обновить головной узел + // Операция извлечения из хвоста очереди } else { - // Операция извлечения из хвоста очереди val = _rear!.val; // Временно сохранить значение хвостового узла // Удалить хвостовой узел ListNode? rPrev = _rear!.prev; - if (rPrev != null) { - rPrev.next = null; - _rear!.prev = null; - } + rPrev!.next = null; + _rear!.prev = null; _rear = rPrev; // Обновить хвостовой узел } _queSize--; // Обновить длину очереди diff --git a/ru/codes/java/chapter_stack_and_queue/linkedlist_deque.java b/ru/codes/java/chapter_stack_and_queue/linkedlist_deque.java index f16d01034..300dcb30b 100644 --- a/ru/codes/java/chapter_stack_and_queue/linkedlist_deque.java +++ b/ru/codes/java/chapter_stack_and_queue/linkedlist_deque.java @@ -76,25 +76,27 @@ class LinkedListDeque { if (isEmpty()) throw new IndexOutOfBoundsException(); int val; + // Если в очереди один элемент, начало и конец указывают на один узел + if (queSize == 1) { + val = front.val; + front = null; + rear = null; + } // Операция извлечения из головы очереди - if (isFront) { + else if (isFront) { val = front.val; // Временно сохранить значение головного узла // Удалить головной узел ListNode fNext = front.next; - if (fNext != null) { - fNext.prev = null; - front.next = null; - } + fNext.prev = null; + front.next = null; front = fNext; // Обновить головной узел // Операция извлечения из хвоста очереди } else { val = rear.val; // Временно сохранить значение хвостового узла // Удалить хвостовой узел ListNode rPrev = rear.prev; - if (rPrev != null) { - rPrev.next = null; - rear.prev = null; - } + rPrev.next = null; + rear.prev = null; rear = rPrev; // Обновить хвостовой узел } queSize--; // Обновить длину очереди diff --git a/ru/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js b/ru/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js index d2293aa66..4158dac26 100644 --- a/ru/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js +++ b/ru/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js @@ -67,13 +67,18 @@ class LinkedListDeque { return null; } const value = this.#rear.val; // Сохранить значение хвостового узла + // Если в очереди один элемент, начало и конец указывают на один узел + if (this.#queSize === 1) { + this.#front = null; + this.#rear = null; + } // Удалить хвостовой узел - let temp = this.#rear.prev; - if (temp !== null) { + else { + let temp = this.#rear.prev; temp.next = null; this.#rear.prev = null; + this.#rear = temp; // Обновить хвостовой узел } - this.#rear = temp; // Обновить хвостовой узел this.#queSize--; return value; } @@ -83,14 +88,19 @@ class LinkedListDeque { if (this.#queSize === 0) { return null; } - const value = this.#front.val; // Сохранить значение хвостового узла + const value = this.#front.val; // Сохраняем значение головного узла + // Если в очереди один элемент, начало и конец указывают на один узел + if (this.#queSize === 1) { + this.#front = null; + this.#rear = null; + } // Удалить головной узел - let temp = this.#front.next; - if (temp !== null) { + else { + let temp = this.#front.next; temp.prev = null; this.#front.next = null; + this.#front = temp; // Обновить головной узел } - this.#front = temp; // Обновить головной узел this.#queSize--; return value; } diff --git a/ru/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt b/ru/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt index 60ee5381d..e9f6dcb3f 100644 --- a/ru/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt +++ b/ru/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt @@ -67,25 +67,27 @@ class LinkedListDeque { if (isEmpty()) throw IndexOutOfBoundsException() val _val: Int + // Если в очереди один элемент, начало и конец указывают на один узел + if (queSize == 1) { + _val = front!!._val + front = null + rear = null + } // Операция извлечения из головы очереди - if (isFront) { + else if (isFront) { _val = front!!._val // Временно сохранить значение головного узла // Удалить головной узел val fNext = front!!.next - if (fNext != null) { - fNext.prev = null - front!!.next = null - } + fNext!!.prev = null + front!!.next = null front = fNext // Обновить головной узел // Операция извлечения из хвоста очереди } else { _val = rear!!._val // Временно сохранить значение хвостового узла // Удалить хвостовой узел val rPrev = rear!!.prev - if (rPrev != null) { - rPrev.next = null - rear!!.prev = null - } + rPrev!!.next = null + rear!!.prev = null rear = rPrev // Обновить хвостовой узел } queSize-- // Обновить длину очереди diff --git a/ru/codes/python/chapter_stack_and_queue/linkedlist_deque.py b/ru/codes/python/chapter_stack_and_queue/linkedlist_deque.py index 481ce94c7..3c8108eea 100644 --- a/ru/codes/python/chapter_stack_and_queue/linkedlist_deque.py +++ b/ru/codes/python/chapter_stack_and_queue/linkedlist_deque.py @@ -64,23 +64,26 @@ class LinkedListDeque: """Операция извлечения из очереди""" if self.is_empty(): raise IndexError("двусторонняя очередь пуста") + # Если в очереди один элемент, начало и конец указывают на один узел + if self._size == 1: + val: int = self._front.val + self._front = None + self._rear = None # Операция извлечения из головы очереди - if is_front: + elif is_front: val: int = self._front.val # Временно сохранить значение головного узла # Удалить головной узел fnext: ListNode | None = self._front.next - if fnext is not None: - fnext.prev = None - self._front.next = None + fnext.prev = None + self._front.next = None self._front = fnext # Обновить головной узел # Операция извлечения из хвоста очереди else: val: int = self._rear.val # Временно сохранить значение хвостового узла # Удалить хвостовой узел rprev: ListNode | None = self._rear.prev - if rprev is not None: - rprev.next = None - self._rear.prev = None + rprev.next = None + self._rear.prev = None self._rear = rprev # Обновить хвостовой узел self._size -= 1 # Обновить длину очереди return val diff --git a/ru/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb b/ru/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb index bf98587d0..a3db6d6f3 100644 --- a/ru/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb +++ b/ru/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb @@ -69,25 +69,26 @@ class LinkedListDeque def pop(is_front) raise IndexError, 'двусторонняя очередь пуста' if is_empty? + # Если в очереди один элемент, начало и конец указывают на один узел + if @size == 1 + val = @front.val + @front = nil + @rear = nil # Операция извлечения из головы очереди - if is_front + elsif is_front val = @front.val # Временно сохранить значение головного узла # Удалить головной узел fnext = @front.next - unless fnext.nil? - fnext.prev = nil - @front.next = nil - end + fnext.prev = nil + @front.next = nil @front = fnext # Обновить головной узел # Операция извлечения из хвоста очереди else val = @rear.val # Временно сохранить значение хвостового узла # Удалить хвостовой узел rprev = @rear.prev - unless rprev.nil? - rprev.next = nil - @rear.prev = nil - end + rprev.next = nil + @rear.prev = nil @rear = rprev # Обновить хвостовой узел end @size -= 1 # Обновить длину очереди diff --git a/ru/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift b/ru/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift index c59585da8..04450fd40 100644 --- a/ru/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift +++ b/ru/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift @@ -76,15 +76,19 @@ class LinkedListDeque { fatalError("двусторонняя очередь пуста") } let val: Int + // Если в очереди один элемент, начало и конец указывают на один узел + if _size == 1 { + val = front!.val + front = nil + rear = nil + } // Операция извлечения из головы очереди - if isFront { + else if isFront { val = front!.val // Временно сохранить значение головного узла // Удалить головной узел let fNext = front?.next - if fNext != nil { - fNext?.prev = nil - front?.next = nil - } + fNext?.prev = nil + front?.next = nil front = fNext // Обновить головной узел } // Операция извлечения из хвоста очереди @@ -92,10 +96,8 @@ class LinkedListDeque { val = rear!.val // Временно сохранить значение хвостового узла // Удалить хвостовой узел let rPrev = rear?.prev - if rPrev != nil { - rPrev?.next = nil - rear?.prev = nil - } + rPrev?.next = nil + rear?.prev = nil rear = rPrev // Обновить хвостовой узел } _size -= 1 // Обновить длину очереди diff --git a/ru/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts b/ru/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts index fdafbf6dd..61b8d47bc 100644 --- a/ru/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts +++ b/ru/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts @@ -66,14 +66,19 @@ class LinkedListDeque { if (this.queSize === 0) { return null; } - const value: number = this.rear.val; // Сохранить значение хвостового узла + const value: number = this.rear!.val; // Сохранить значение хвостового узла + // Если в очереди один элемент, начало и конец указывают на один узел + if (this.queSize === 1) { + this.front = null; + this.rear = null; + } // Удалить хвостовой узел - let temp: ListNode = this.rear.prev; - if (temp !== null) { - temp.next = null; - this.rear.prev = null; + else { + let temp: ListNode = this.rear!.prev; + temp!.next = null; + this.rear!.prev = null; + this.rear = temp; // Обновить хвостовой узел } - this.rear = temp; // Обновить хвостовой узел this.queSize--; return value; } @@ -83,14 +88,19 @@ class LinkedListDeque { if (this.queSize === 0) { return null; } - const value: number = this.front.val; // Сохранить значение хвостового узла + const value: number = this.front.val; // Сохраняем значение головного узла + // Если в очереди один элемент, начало и конец указывают на один узел + if (this.queSize === 1) { + this.front = null; + this.rear = null; + } // Удалить головной узел - let temp: ListNode = this.front.next; - if (temp !== null) { - temp.prev = null; - this.front.next = null; + else { + let temp: ListNode = this.front!.next; + temp!.prev = null; + this.front!.next = null; + this.front = temp; // Обновить головной узел } - this.front = temp; // Обновить головной узел this.queSize--; return value; } diff --git a/ru/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig b/ru/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig index 268b47516..dfcaf0e62 100644 --- a/ru/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig +++ b/ru/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig @@ -99,25 +99,27 @@ pub fn LinkedListDeque(comptime T: type) type { pub fn pop(self: *Self, is_front: bool) T { if (self.isEmpty()) @panic("двусторонняя очередь пуста"); var val: T = undefined; + // Если в очереди один элемент, начало и конец указывают на один узел + if (self.que_size == 1) { + val = self.front.?.val; + self.front = null; + self.rear = null; + } // Операция извлечения из головы очереди - if (is_front) { + else if (is_front) { val = self.front.?.val; // Временно сохранить значение головного узла // Удалить головной узел var fNext = self.front.?.next; - if (fNext != null) { - fNext.?.prev = null; - self.front.?.next = null; - } + fNext.?.prev = null; + self.front.?.next = null; self.front = fNext; // Обновить головной узел // Операция извлечения из хвоста очереди } else { val = self.rear.?.val; // Временно сохранить значение хвостового узла // Удалить хвостовой узел var rPrev = self.rear.?.prev; - if (rPrev != null) { - rPrev.?.next = null; - self.rear.?.prev = null; - } + rPrev.?.next = null; + self.rear.?.prev = null; self.rear = rPrev; // Обновить хвостовой узел } self.que_size -= 1; // Обновить длину очереди diff --git a/ru/docs/chapter_array_and_linkedlist/array.md b/ru/docs/chapter_array_and_linkedlist/array.md index 52b835309..6b5b18a38 100644 --- a/ru/docs/chapter_array_and_linkedlist/array.md +++ b/ru/docs/chapter_array_and_linkedlist/array.md @@ -23,10 +23,10 @@ ```cpp title="array.cpp" /* Инициализация массива */ // Хранится в стеке - int arr[5]; + int arr[5] = {}; int nums[5] = { 1, 3, 2, 5, 4 }; // Хранится в куче (требуется ручное освобождение памяти) - int* arr1 = new int[5]; + int* arr1 = new int[5] {}; int* nums1 = new int[5] { 1, 3, 2, 5, 4 }; ``` diff --git a/zh-hant/codes/c/chapter_stack_and_queue/linkedlist_deque.c b/zh-hant/codes/c/chapter_stack_and_queue/linkedlist_deque.c index ff3e52b9d..2cdd7b9ee 100644 --- a/zh-hant/codes/c/chapter_stack_and_queue/linkedlist_deque.c +++ b/zh-hant/codes/c/chapter_stack_and_queue/linkedlist_deque.c @@ -115,14 +115,19 @@ int pop(LinkedListDeque *deque, bool isFront) { if (empty(deque)) return -1; int val; + // 佇列長度為 1 時, 頭尾指向同一節點 + if (deque->queSize == 1) { + val = deque->front->val; + delDoublyListNode(deque->front); // 指向同一節點,釋放其一即可 + deque->front = NULL; + deque->rear = NULL; + } // 佇列首出列操作 - if (isFront) { + else if (isFront) { val = peekFirst(deque); // 暫存頭節點值 DoublyListNode *fNext = deque->front->next; - if (fNext) { - fNext->prev = NULL; - deque->front->next = NULL; - } + fNext->prev = NULL; + deque->front->next = NULL; delDoublyListNode(deque->front); deque->front = fNext; // 更新頭節點 } @@ -130,10 +135,8 @@ int pop(LinkedListDeque *deque, bool isFront) { else { val = peekLast(deque); // 暫存尾節點值 DoublyListNode *rPrev = deque->rear->prev; - if (rPrev) { - rPrev->next = NULL; - deque->rear->prev = NULL; - } + rPrev->next = NULL; + deque->rear->prev = NULL; delDoublyListNode(deque->rear); deque->rear = rPrev; // 更新尾節點 } diff --git a/zh-hant/codes/c/utils/print_util.h b/zh-hant/codes/c/utils/print_util.h index aab7f140a..bbecc5723 100644 --- a/zh-hant/codes/c/utils/print_util.h +++ b/zh-hant/codes/c/utils/print_util.h @@ -64,8 +64,8 @@ typedef struct Trunk { Trunk *newTrunk(Trunk *prev, char *str) { Trunk *trunk = (Trunk *)malloc(sizeof(Trunk)); trunk->prev = prev; - trunk->str = (char *)malloc(sizeof(char) * 10); - strcpy(trunk->str, str); + trunk->str = (char *)malloc(strlen(str) + 1); + snprintf(trunk->str, strlen(str) + 1, "%s", str); return trunk; } diff --git a/zh-hant/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp b/zh-hant/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp index 1767609eb..944cce0f4 100644 --- a/zh-hant/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp +++ b/zh-hant/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp @@ -84,15 +84,20 @@ class LinkedListDeque { if (isEmpty()) throw out_of_range("佇列為空"); int val; + // 佇列長度為 1 時, 頭尾指向同一節點 + if (queSize == 1) { + val = front->val; + delete front; // 指向同一節點,釋放其一即可 + front = nullptr; + rear = nullptr; + } // 佇列首出列操作 - if (isFront) { + else if (isFront) { val = front->val; // 暫存頭節點值 // 刪除頭節點 DoublyListNode *fNext = front->next; - if (fNext != nullptr) { - fNext->prev = nullptr; - front->next = nullptr; - } + fNext->prev = nullptr; + front->next = nullptr; delete front; front = fNext; // 更新頭節點 // 佇列尾出列操作 @@ -100,10 +105,8 @@ class LinkedListDeque { val = rear->val; // 暫存尾節點值 // 刪除尾節點 DoublyListNode *rPrev = rear->prev; - if (rPrev != nullptr) { - rPrev->next = nullptr; - rear->prev = nullptr; - } + rPrev->next = nullptr; + rear->prev = nullptr; delete rear; rear = rPrev; // 更新尾節點 } diff --git a/zh-hant/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs b/zh-hant/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs index 1379a4182..a0c2a1a28 100644 --- a/zh-hant/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs +++ b/zh-hant/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs @@ -74,26 +74,28 @@ public class LinkedListDeque { if (IsEmpty()) throw new Exception(); int? val; + // 佇列長度為 1 時, 頭尾指向同一節點 + if (queSize == 1) { + val = front!.val; + front = null; + rear = null; + } // 佇列首出列操作 - if (isFront) { - val = front?.val; // 暫存頭節點值 + else if (isFront) { + val = front!.val; // 暫存頭節點值 // 刪除頭節點 ListNode? fNext = front?.next; - if (fNext != null) { - fNext.prev = null; - front!.next = null; - } + fNext!.prev = null; + front.next = null; front = fNext; // 更新頭節點 } // 佇列尾出列操作 else { - val = rear?.val; // 暫存尾節點值 + val = rear!.val; // 暫存尾節點值 // 刪除尾節點 ListNode? rPrev = rear?.prev; - if (rPrev != null) { - rPrev.next = null; - rear!.prev = null; - } + rPrev!.next = null; + rear.prev = null; rear = rPrev; // 更新尾節點 } diff --git a/zh-hant/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart b/zh-hant/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart index 547cee251..0ef491737 100644 --- a/zh-hant/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart +++ b/zh-hant/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart @@ -73,25 +73,27 @@ class LinkedListDeque { return null; } final int val; - if (isFront) { - // 佇列首出列操作 + // 佇列長度為 1 時, 頭尾指向同一節點 + if (_queSize == 1) { + val = _front!.val; + _front = null; + _rear = null; + } + // 佇列首出列操作 + else if (isFront) { val = _front!.val; // 暫存頭節點值 // 刪除頭節點 ListNode? fNext = _front!.next; - if (fNext != null) { - fNext.prev = null; - _front!.next = null; - } + fNext!.prev = null; + _front!.next = null; _front = fNext; // 更新頭節點 + // 佇列尾出列操作 } else { - // 佇列尾出列操作 val = _rear!.val; // 暫存尾節點值 // 刪除尾節點 ListNode? rPrev = _rear!.prev; - if (rPrev != null) { - rPrev.next = null; - _rear!.prev = null; - } + rPrev!.next = null; + _rear!.prev = null; _rear = rPrev; // 更新尾節點 } _queSize--; // 更新佇列長度 diff --git a/zh-hant/codes/java/chapter_stack_and_queue/linkedlist_deque.java b/zh-hant/codes/java/chapter_stack_and_queue/linkedlist_deque.java index c3168ae33..6889a8c26 100644 --- a/zh-hant/codes/java/chapter_stack_and_queue/linkedlist_deque.java +++ b/zh-hant/codes/java/chapter_stack_and_queue/linkedlist_deque.java @@ -76,25 +76,27 @@ class LinkedListDeque { if (isEmpty()) throw new IndexOutOfBoundsException(); int val; + // 佇列長度為 1 時, 頭尾指向同一節點 + if (queSize == 1) { + val = front.val; + front = null; + rear = null; + } // 佇列首出列操作 - if (isFront) { + else if (isFront) { val = front.val; // 暫存頭節點值 // 刪除頭節點 ListNode fNext = front.next; - if (fNext != null) { - fNext.prev = null; - front.next = null; - } + fNext.prev = null; + front.next = null; front = fNext; // 更新頭節點 // 佇列尾出列操作 } else { val = rear.val; // 暫存尾節點值 // 刪除尾節點 ListNode rPrev = rear.prev; - if (rPrev != null) { - rPrev.next = null; - rear.prev = null; - } + rPrev.next = null; + rear.prev = null; rear = rPrev; // 更新尾節點 } queSize--; // 更新佇列長度 diff --git a/zh-hant/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js b/zh-hant/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js index f640433e5..c66a161da 100644 --- a/zh-hant/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js +++ b/zh-hant/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js @@ -67,13 +67,18 @@ class LinkedListDeque { return null; } const value = this.#rear.val; // 儲存尾節點值 + // 佇列長度為 1 時, 頭尾指向同一節點 + if (this.#queSize === 1) { + this.#front = null; + this.#rear = null; + } // 刪除尾節點 - let temp = this.#rear.prev; - if (temp !== null) { + else { + let temp = this.#rear.prev; temp.next = null; this.#rear.prev = null; + this.#rear = temp; // 更新尾節點 } - this.#rear = temp; // 更新尾節點 this.#queSize--; return value; } @@ -83,14 +88,19 @@ class LinkedListDeque { if (this.#queSize === 0) { return null; } - const value = this.#front.val; // 儲存尾節點值 + const value = this.#front.val; // 儲存頭節點值 + // 佇列長度為 1 時, 頭尾指向同一節點 + if (this.#queSize === 1) { + this.#front = null; + this.#rear = null; + } // 刪除頭節點 - let temp = this.#front.next; - if (temp !== null) { + else { + let temp = this.#front.next; temp.prev = null; this.#front.next = null; + this.#front = temp; // 更新頭節點 } - this.#front = temp; // 更新頭節點 this.#queSize--; return value; } diff --git a/zh-hant/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt b/zh-hant/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt index d5ae2b229..54a440a26 100644 --- a/zh-hant/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt +++ b/zh-hant/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt @@ -67,25 +67,27 @@ class LinkedListDeque { if (isEmpty()) throw IndexOutOfBoundsException() val _val: Int + // 佇列長度為 1 時, 頭尾指向同一節點 + if (queSize == 1) { + _val = front!!._val + front = null + rear = null + } // 佇列首出列操作 - if (isFront) { + else if (isFront) { _val = front!!._val // 暫存頭節點值 // 刪除頭節點 val fNext = front!!.next - if (fNext != null) { - fNext.prev = null - front!!.next = null - } + fNext!!.prev = null + front!!.next = null front = fNext // 更新頭節點 // 佇列尾出列操作 } else { _val = rear!!._val // 暫存尾節點值 // 刪除尾節點 val rPrev = rear!!.prev - if (rPrev != null) { - rPrev.next = null - rear!!.prev = null - } + rPrev!!.next = null + rear!!.prev = null rear = rPrev // 更新尾節點 } queSize-- // 更新佇列長度 diff --git a/zh-hant/codes/python/chapter_stack_and_queue/linkedlist_deque.py b/zh-hant/codes/python/chapter_stack_and_queue/linkedlist_deque.py index c7b174272..68674e6a6 100644 --- a/zh-hant/codes/python/chapter_stack_and_queue/linkedlist_deque.py +++ b/zh-hant/codes/python/chapter_stack_and_queue/linkedlist_deque.py @@ -64,23 +64,26 @@ class LinkedListDeque: """出列操作""" if self.is_empty(): raise IndexError("雙向佇列為空") + # 佇列長度為 1 時, 頭尾指向同一節點 + if self._size == 1: + val: int = self._front.val + self._front = None + self._rear = None # 佇列首出列操作 - if is_front: + elif is_front: val: int = self._front.val # 暫存頭節點值 # 刪除頭節點 fnext: ListNode | None = self._front.next - if fnext is not None: - fnext.prev = None - self._front.next = None + fnext.prev = None + self._front.next = None self._front = fnext # 更新頭節點 # 佇列尾出列操作 else: val: int = self._rear.val # 暫存尾節點值 # 刪除尾節點 rprev: ListNode | None = self._rear.prev - if rprev is not None: - rprev.next = None - self._rear.prev = None + rprev.next = None + self._rear.prev = None self._rear = rprev # 更新尾節點 self._size -= 1 # 更新佇列長度 return val diff --git a/zh-hant/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb b/zh-hant/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb index 6bad53f1a..66a9cee9e 100644 --- a/zh-hant/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb +++ b/zh-hant/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb @@ -69,25 +69,26 @@ class LinkedListDeque def pop(is_front) raise IndexError, '雙向佇列為空' if is_empty? + # 佇列長度為 1 時, 頭尾指向同一節點 + if @size == 1 + val = @front.val + @front = nil + @rear = nil # 佇列首出列操作 - if is_front + elsif is_front val = @front.val # 暫存頭節點值 # 刪除頭節點 fnext = @front.next - unless fnext.nil? - fnext.prev = nil - @front.next = nil - end + fnext.prev = nil + @front.next = nil @front = fnext # 更新頭節點 # 佇列尾出列操作 else val = @rear.val # 暫存尾節點值 # 刪除尾節點 rprev = @rear.prev - unless rprev.nil? - rprev.next = nil - @rear.prev = nil - end + rprev.next = nil + @rear.prev = nil @rear = rprev # 更新尾節點 end @size -= 1 # 更新佇列長度 diff --git a/zh-hant/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift b/zh-hant/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift index e3055ba31..fa2cd2b39 100644 --- a/zh-hant/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift +++ b/zh-hant/codes/swift/chapter_stack_and_queue/linkedlist_deque.swift @@ -76,15 +76,19 @@ class LinkedListDeque { fatalError("雙向佇列為空") } let val: Int + // 佇列長度為 1 時, 頭尾指向同一節點 + if _size == 1 { + val = front!.val + front = nil + rear = nil + } // 佇列首出列操作 - if isFront { + else if isFront { val = front!.val // 暫存頭節點值 // 刪除頭節點 let fNext = front?.next - if fNext != nil { - fNext?.prev = nil - front?.next = nil - } + fNext?.prev = nil + front?.next = nil front = fNext // 更新頭節點 } // 佇列尾出列操作 @@ -92,10 +96,8 @@ class LinkedListDeque { val = rear!.val // 暫存尾節點值 // 刪除尾節點 let rPrev = rear?.prev - if rPrev != nil { - rPrev?.next = nil - rear?.prev = nil - } + rPrev?.next = nil + rear?.prev = nil rear = rPrev // 更新尾節點 } _size -= 1 // 更新佇列長度 diff --git a/zh-hant/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts b/zh-hant/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts index 47973ca95..dee0629d6 100644 --- a/zh-hant/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts +++ b/zh-hant/codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts @@ -66,14 +66,19 @@ class LinkedListDeque { if (this.queSize === 0) { return null; } - const value: number = this.rear.val; // 儲存尾節點值 + const value: number = this.rear!.val; // 儲存尾節點值 + // 佇列長度為 1 時, 頭尾指向同一節點 + if (this.queSize === 1) { + this.front = null; + this.rear = null; + } // 刪除尾節點 - let temp: ListNode = this.rear.prev; - if (temp !== null) { - temp.next = null; - this.rear.prev = null; + else { + let temp: ListNode = this.rear!.prev; + temp!.next = null; + this.rear!.prev = null; + this.rear = temp; // 更新尾節點 } - this.rear = temp; // 更新尾節點 this.queSize--; return value; } @@ -83,14 +88,19 @@ class LinkedListDeque { if (this.queSize === 0) { return null; } - const value: number = this.front.val; // 儲存尾節點值 + const value: number = this.front.val; // 儲存頭節點值 + // 佇列長度為 1 時, 頭尾指向同一節點 + if (this.queSize === 1) { + this.front = null; + this.rear = null; + } // 刪除頭節點 - let temp: ListNode = this.front.next; - if (temp !== null) { - temp.prev = null; - this.front.next = null; + else { + let temp: ListNode = this.front!.next; + temp!.prev = null; + this.front!.next = null; + this.front = temp; // 更新頭節點 } - this.front = temp; // 更新頭節點 this.queSize--; return value; } diff --git a/zh-hant/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig b/zh-hant/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig index 45639b437..7e1b17a85 100644 --- a/zh-hant/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig +++ b/zh-hant/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig @@ -99,25 +99,27 @@ pub fn LinkedListDeque(comptime T: type) type { pub fn pop(self: *Self, is_front: bool) T { if (self.isEmpty()) @panic("雙向佇列為空"); var val: T = undefined; + // 佇列長度為 1 時, 頭尾指向同一節點 + if (self.que_size == 1) { + val = self.front.?.val; + self.front = null; + self.rear = null; + } // 佇列首出列操作 - if (is_front) { + else if (is_front) { val = self.front.?.val; // 暫存頭節點值 // 刪除頭節點 var fNext = self.front.?.next; - if (fNext != null) { - fNext.?.prev = null; - self.front.?.next = null; - } + fNext.?.prev = null; + self.front.?.next = null; self.front = fNext; // 更新頭節點 // 佇列尾出列操作 } else { val = self.rear.?.val; // 暫存尾節點值 // 刪除尾節點 var rPrev = self.rear.?.prev; - if (rPrev != null) { - rPrev.?.next = null; - self.rear.?.prev = null; - } + rPrev.?.next = null; + self.rear.?.prev = null; self.rear = rPrev; // 更新尾節點 } self.que_size -= 1; // 更新佇列長度 diff --git a/zh-hant/docs/chapter_array_and_linkedlist/array.md b/zh-hant/docs/chapter_array_and_linkedlist/array.md index 49183d4d1..0e229d27c 100755 --- a/zh-hant/docs/chapter_array_and_linkedlist/array.md +++ b/zh-hant/docs/chapter_array_and_linkedlist/array.md @@ -23,10 +23,10 @@ ```cpp title="array.cpp" /* 初始化陣列 */ // 儲存在堆疊上 - int arr[5]; + int arr[5] = {}; int nums[5] = { 1, 3, 2, 5, 4 }; // 儲存在堆積上(需要手動釋放空間) - int* arr1 = new int[5]; + int* arr1 = new int[5] {}; int* nums1 = new int[5] { 1, 3, 2, 5, 4 }; ```