Skip to content

Commit

Permalink
fine tune
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Dec 24, 2022
1 parent 49fe239 commit 1790916
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
8 changes: 2 additions & 6 deletions codes/javascript/chapter_stack_and_queue/linkedlist_stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ListNode = require("../include/ListNode");

/* 基于链表实现的栈 */
class LinkedListStack {
#stackPeek; // 将头结点作为栈顶
#stackPeek; // 将头结点作为栈顶
#stkSize = 0; // 栈的长度

constructor() {
Expand Down Expand Up @@ -36,19 +36,15 @@ class LinkedListStack {
/* 出栈 */
pop() {
const num = this.peek();
if (!this.#stackPeek) {
throw new Error("栈为空!");
}
this.#stackPeek = this.#stackPeek.next;
this.#stkSize--;
return num;
}

/* 访问栈顶元素 */
peek() {
if (!this.#stackPeek) {
if (!this.#stackPeek)
throw new Error("栈为空!");
}
return this.#stackPeek.val;
}

Expand Down
12 changes: 5 additions & 7 deletions codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,19 +36,17 @@ 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;
}

/* 访问栈顶元素 */
peek(): number {
if (!this.stackPeek) {
throw new Error("栈为空!");
}
if (!this.stackPeek)
throw new Error("栈为空");
return this.stackPeek.val;
}

Expand Down

0 comments on commit 1790916

Please sign in to comment.