From 17909162fc922e5acab65a5cd9ef2aa818cf508f Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Sat, 24 Dec 2022 12:53:16 +0800 Subject: [PATCH] fine tune --- .../chapter_stack_and_queue/linkedlist_stack.js | 8 ++------ .../chapter_stack_and_queue/linkedlist_stack.ts | 12 +++++------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js index 951616d2e..c6e9f27ce 100644 --- a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js +++ b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js @@ -8,7 +8,7 @@ const ListNode = require("../include/ListNode"); /* 基于链表实现的栈 */ class LinkedListStack { - #stackPeek; // 将头结点作为栈顶 + #stackPeek; // 将头结点作为栈顶 #stkSize = 0; // 栈的长度 constructor() { @@ -36,9 +36,6 @@ class LinkedListStack { /* 出栈 */ pop() { const num = this.peek(); - if (!this.#stackPeek) { - throw new Error("栈为空!"); - } this.#stackPeek = this.#stackPeek.next; this.#stkSize--; return num; @@ -46,9 +43,8 @@ class LinkedListStack { /* 访问栈顶元素 */ peek() { - if (!this.#stackPeek) { + if (!this.#stackPeek) throw new Error("栈为空!"); - } return this.#stackPeek.val; } diff --git a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts index cdc487894..1df4b4997 100644 --- a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts +++ b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts @@ -9,7 +9,7 @@ import ListNode from "../module/ListNode" /* 基于链表实现的栈 */ class LinkedListStack { private stackPeek: ListNode | null; // 将头结点作为栈顶 - private stkSize: number = 0; // 栈的长度 + private stkSize: number = 0; // 栈的长度 constructor() { this.stackPeek = null; @@ -36,9 +36,8 @@ class LinkedListStack { /* 出栈 */ pop(): number { const num = this.peek(); - if (!this.stackPeek) { - throw new Error("栈为空!"); - } + if (!this.stackPeek) + throw new Error("栈为空"); this.stackPeek = this.stackPeek.next; this.stkSize--; return num; @@ -46,9 +45,8 @@ class LinkedListStack { /* 访问栈顶元素 */ peek(): number { - if (!this.stackPeek) { - throw new Error("栈为空!"); - } + if (!this.stackPeek) + throw new Error("栈为空"); return this.stackPeek.val; }