mirror of
https://github.com/krahets/hello-algo.git
synced 2026-10-29 03:35:13 +08:00
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.
This commit is contained in:
4
.github/workflows/java.yml
vendored
4
.github/workflows/java.yml
vendored
@@ -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"]
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -66,14 +66,19 @@ class LinkedListDeque {
|
||||
if (this.queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value: number = this.rear.val; // Store tail node value
|
||||
// Update tail node
|
||||
let temp: ListNode = this.rear.prev;
|
||||
if (temp !== null) {
|
||||
temp.next = null;
|
||||
this.rear.prev = null;
|
||||
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
|
||||
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
|
||||
// Delete head node
|
||||
let temp: ListNode = this.front.next;
|
||||
if (temp !== null) {
|
||||
temp.prev = null;
|
||||
this.front.next = null;
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
```
|
||||
|
||||
|
||||
@@ -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; // 末尾ノードを更新する
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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; // 末尾ノードを更新する
|
||||
}
|
||||
|
||||
@@ -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; // 末尾ノードを更新する
|
||||
}
|
||||
|
||||
|
||||
@@ -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--; // キューの長さを更新
|
||||
|
||||
@@ -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--; // キューの長さを更新
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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-- // キューの長さを更新
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 # キューの長さを更新
|
||||
|
||||
@@ -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 // キューの長さを更新
|
||||
|
||||
@@ -66,14 +66,19 @@ class LinkedListDeque {
|
||||
if (this.queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value: number = this.rear.val; // 末尾ノードの値を保存する
|
||||
// 末尾ノードを削除
|
||||
let temp: ListNode = this.rear.prev;
|
||||
if (temp !== null) {
|
||||
temp.next = null;
|
||||
this.rear.prev = null;
|
||||
const value: number = this.rear!.val; // 末尾ノードの値を保存する
|
||||
// 要素が1つの場合、先頭と末尾は同じノードを指す
|
||||
if (this.queSize === 1) {
|
||||
this.front = null;
|
||||
this.rear = 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; // 末尾ノードの値を保存する
|
||||
// 先頭ノードを削除
|
||||
let temp: ListNode = this.front.next;
|
||||
if (temp !== null) {
|
||||
temp.prev = null;
|
||||
this.front.next = null;
|
||||
const value: number = this.front.val; // 先頭ノードの値を保存
|
||||
// 要素が1つの場合、先頭と末尾は同じノードを指す
|
||||
if (this.queSize === 1) {
|
||||
this.front = null;
|
||||
this.rear = 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;
|
||||
}
|
||||
|
||||
@@ -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; // キューの長さを更新
|
||||
|
||||
@@ -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 };
|
||||
```
|
||||
|
||||
|
||||
@@ -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; // Обновить хвостовой узел
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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; // Обновить хвостовой узел
|
||||
}
|
||||
|
||||
@@ -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; // Обновить хвостовой узел
|
||||
}
|
||||
|
||||
|
||||
@@ -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--; // Обновить длину очереди
|
||||
|
||||
@@ -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--; // Обновить длину очереди
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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-- // Обновить длину очереди
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 # Обновить длину очереди
|
||||
|
||||
@@ -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 // Обновить длину очереди
|
||||
|
||||
@@ -66,14 +66,19 @@ class LinkedListDeque {
|
||||
if (this.queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value: number = this.rear.val; // Сохранить значение хвостового узла
|
||||
// Удалить хвостовой узел
|
||||
let temp: ListNode = this.rear.prev;
|
||||
if (temp !== null) {
|
||||
temp.next = null;
|
||||
this.rear.prev = null;
|
||||
const value: number = this.rear!.val; // Сохранить значение хвостового узла
|
||||
// Если в очереди один элемент, начало и конец указывают на один узел
|
||||
if (this.queSize === 1) {
|
||||
this.front = null;
|
||||
this.rear = 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; // Сохранить значение хвостового узла
|
||||
// Удалить головной узел
|
||||
let temp: ListNode = this.front.next;
|
||||
if (temp !== null) {
|
||||
temp.prev = null;
|
||||
this.front.next = null;
|
||||
const value: number = this.front.val; // Сохраняем значение головного узла
|
||||
// Если в очереди один элемент, начало и конец указывают на один узел
|
||||
if (this.queSize === 1) {
|
||||
this.front = null;
|
||||
this.rear = 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;
|
||||
}
|
||||
|
||||
@@ -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; // Обновить длину очереди
|
||||
|
||||
@@ -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 };
|
||||
```
|
||||
|
||||
|
||||
@@ -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; // 更新尾節點
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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; // 更新尾節點
|
||||
}
|
||||
|
||||
@@ -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; // 更新尾節點
|
||||
}
|
||||
|
||||
|
||||
@@ -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--; // 更新佇列長度
|
||||
|
||||
@@ -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--; // 更新佇列長度
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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-- // 更新佇列長度
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 # 更新佇列長度
|
||||
|
||||
@@ -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 // 更新佇列長度
|
||||
|
||||
@@ -66,14 +66,19 @@ class LinkedListDeque {
|
||||
if (this.queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value: number = this.rear.val; // 儲存尾節點值
|
||||
// 刪除尾節點
|
||||
let temp: ListNode = this.rear.prev;
|
||||
if (temp !== null) {
|
||||
temp.next = null;
|
||||
this.rear.prev = null;
|
||||
const value: number = this.rear!.val; // 儲存尾節點值
|
||||
// 佇列長度為 1 時, 頭尾指向同一節點
|
||||
if (this.queSize === 1) {
|
||||
this.front = null;
|
||||
this.rear = 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; // 儲存尾節點值
|
||||
// 刪除頭節點
|
||||
let temp: ListNode = this.front.next;
|
||||
if (temp !== null) {
|
||||
temp.prev = null;
|
||||
this.front.next = null;
|
||||
const value: number = this.front.val; // 儲存頭節點值
|
||||
// 佇列長度為 1 時, 頭尾指向同一節點
|
||||
if (this.queSize === 1) {
|
||||
this.front = null;
|
||||
this.rear = 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;
|
||||
}
|
||||
|
||||
@@ -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; // 更新佇列長度
|
||||
|
||||
@@ -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 };
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user