diff --git a/codes/javascript/chapter_stack_and_queue/array_queue.js b/codes/javascript/chapter_stack_and_queue/array_queue.js index a57fbf4a4..4e05de13a 100644 --- a/codes/javascript/chapter_stack_and_queue/array_queue.js +++ b/codes/javascript/chapter_stack_and_queue/array_queue.js @@ -25,7 +25,7 @@ class ArrayQueue { } /* 判断队列是否为空 */ - empty() { + isEmpty() { return this.#queSize === 0; } @@ -54,7 +54,7 @@ class ArrayQueue { /* 访问队首元素 */ peek() { - if (this.empty()) throw new Error('队列为空'); + if (this.isEmpty()) throw new Error('队列为空'); return this.#nums[this.#front]; } @@ -95,8 +95,8 @@ const size = queue.size; console.log('队列长度 size = ' + size); /* 判断队列是否为空 */ -const empty = queue.empty(); -console.log('队列是否为空 = ' + empty); +const isEmpty = queue.isEmpty(); +console.log('队列是否为空 = ' + isEmpty); /* 测试环形数组 */ for (let i = 0; i < 10; i++) { diff --git a/codes/javascript/chapter_stack_and_queue/array_stack.js b/codes/javascript/chapter_stack_and_queue/array_stack.js index 18bb9d60f..7fceeb182 100644 --- a/codes/javascript/chapter_stack_and_queue/array_stack.js +++ b/codes/javascript/chapter_stack_and_queue/array_stack.js @@ -17,7 +17,7 @@ class ArrayStack { } /* 判断栈是否为空 */ - empty() { + isEmpty() { return this.#stack.length === 0; } @@ -28,13 +28,13 @@ class ArrayStack { /* 出栈 */ pop() { - if (this.empty()) throw new Error('栈为空'); + if (this.isEmpty()) throw new Error('栈为空'); return this.#stack.pop(); } /* 访问栈顶元素 */ top() { - if (this.empty()) throw new Error('栈为空'); + if (this.isEmpty()) throw new Error('栈为空'); return this.#stack[this.#stack.length - 1]; } @@ -71,5 +71,5 @@ const size = stack.size; console.log('栈的长度 size = ' + size); /* 判断是否为空 */ -const empty = stack.empty(); -console.log('栈是否为空 = ' + empty); +const isEmpty = stack.isEmpty(); +console.log('栈是否为空 = ' + isEmpty); diff --git a/codes/typescript/chapter_stack_and_queue/array_queue.ts b/codes/typescript/chapter_stack_and_queue/array_queue.ts index d8898b12f..71ab27c8d 100644 --- a/codes/typescript/chapter_stack_and_queue/array_queue.ts +++ b/codes/typescript/chapter_stack_and_queue/array_queue.ts @@ -26,7 +26,7 @@ class ArrayQueue { } /* 判断队列是否为空 */ - empty(): boolean { + isEmpty(): boolean { return this.queSize === 0; } @@ -55,7 +55,7 @@ class ArrayQueue { /* 访问队首元素 */ peek(): number { - if (this.empty()) throw new Error('队列为空'); + if (this.isEmpty()) throw new Error('队列为空'); return this.nums[this.front]; } @@ -96,8 +96,8 @@ const size = queue.size; console.log('队列长度 size = ' + size); /* 判断队列是否为空 */ -const empty = queue.empty(); -console.log('队列是否为空 = ' + empty); +const isEmpty = queue.isEmpty(); +console.log('队列是否为空 = ' + isEmpty); /* 测试环形数组 */ for (let i = 0; i < 10; i++) { diff --git a/codes/typescript/chapter_stack_and_queue/array_stack.ts b/codes/typescript/chapter_stack_and_queue/array_stack.ts index adb85d970..9ff140c57 100644 --- a/codes/typescript/chapter_stack_and_queue/array_stack.ts +++ b/codes/typescript/chapter_stack_and_queue/array_stack.ts @@ -17,7 +17,7 @@ class ArrayStack { } /* 判断栈是否为空 */ - empty(): boolean { + isEmpty(): boolean { return this.stack.length === 0; } @@ -28,13 +28,13 @@ class ArrayStack { /* 出栈 */ pop(): number | undefined { - if (this.empty()) throw new Error('栈为空'); + if (this.isEmpty()) throw new Error('栈为空'); return this.stack.pop(); } /* 访问栈顶元素 */ top(): number | undefined { - if (this.empty()) throw new Error('栈为空'); + if (this.isEmpty()) throw new Error('栈为空'); return this.stack[this.stack.length - 1]; } @@ -71,7 +71,7 @@ const size = stack.size; console.log('栈的长度 size = ' + size); /* 判断是否为空 */ -const empty = stack.empty(); -console.log('栈是否为空 = ' + empty); +const isEmpty = stack.isEmpty(); +console.log('栈是否为空 = ' + isEmpty); export {};