From 82bbdd444d99170d91c844e098cfe1b4df10c0da Mon Sep 17 00:00:00 2001 From: krahets Date: Sun, 12 Mar 2023 04:14:36 +0800 Subject: [PATCH] Update the chapter of stack and queue. --- codes/cpp/chapter_stack_and_queue/stack.cpp | 2 +- codes/java/chapter_stack_and_queue/stack.java | 17 +++++----- docs/chapter_stack_and_queue/deque.md | 4 +-- docs/chapter_stack_and_queue/queue.md | 6 ++-- docs/chapter_stack_and_queue/stack.md | 33 +++++++++---------- 5 files changed, 27 insertions(+), 35 deletions(-) diff --git a/codes/cpp/chapter_stack_and_queue/stack.cpp b/codes/cpp/chapter_stack_and_queue/stack.cpp index 7bbfd6b9f..98d62aa10 100644 --- a/codes/cpp/chapter_stack_and_queue/stack.cpp +++ b/codes/cpp/chapter_stack_and_queue/stack.cpp @@ -26,7 +26,7 @@ int main() { cout << "栈顶元素 top = " << top << endl; /* 元素出栈 */ - stack.pop(); + stack.pop(); // 无返回值 cout << "出栈元素 pop = " << top << ",出栈后 stack = "; PrintUtil::printStack(stack); diff --git a/codes/java/chapter_stack_and_queue/stack.java b/codes/java/chapter_stack_and_queue/stack.java index 52f44a0de..a5d35bef3 100644 --- a/codes/java/chapter_stack_and_queue/stack.java +++ b/codes/java/chapter_stack_and_queue/stack.java @@ -11,23 +11,22 @@ import java.util.*; public class stack { public static void main(String[] args) { /* 初始化栈 */ - // 在 Java 中,推荐将 ArrayList 当作栈来使用 - List stack = new ArrayList<>(); + Stack stack = new Stack<>(); /* 元素入栈 */ - stack.add(1); - stack.add(3); - stack.add(2); - stack.add(5); - stack.add(4); + stack.push(1); + stack.push(3); + stack.push(2); + stack.push(5); + stack.push(4); System.out.println("栈 stack = " + stack); /* 访问栈顶元素 */ - int peek = stack.get(stack.size() - 1); + int peek = stack.peek(); System.out.println("栈顶元素 peek = " + peek); /* 元素出栈 */ - int pop = stack.remove(stack.size() - 1); + int pop = stack.pop(); System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + stack); /* 获取栈的长度 */ diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index c6489649b..a44954064 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -6,7 +6,7 @@ ## 双向队列常用操作 -双向队列的常用操作见下表,方法名需根据特定语言来确定。 +双向队列的常用操作见下表,方法名需根据语言来确定,此处以 Java 为例。
@@ -18,8 +18,6 @@ | pollLast() | 删除队尾元素 | $O(1)$ | | peekFirst() | 访问队首元素 | $O(1)$ | | peekLast() | 访问队尾元素 | $O(1)$ | -| size() | 获取队列的长度 | $O(1)$ | -| isEmpty() | 判断队列是否为空 | $O(1)$ |
diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index d665af8c5..fb28fd6a8 100755 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -8,7 +8,7 @@ ## 队列常用操作 -队列的常用操作见下表,方法名需根据特定语言来确定。 +队列的常用操作见下表,方法名需根据语言来确定,此处以 Java 为例。
@@ -16,9 +16,7 @@ | --------- | -------------------------- | -------- | | push() | 元素入队,即将元素添加至队尾 | $O(1)$ | | poll() | 队首元素出队 | $O(1)$ | -| front() | 访问队首元素 | $O(1)$ | -| size() | 获取队列的长度 | $O(1)$ | -| isEmpty() | 判断队列是否为空 | $O(1)$ | +| peek() | 访问队首元素 | $O(1)$ |
diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 989759e12..90bc2c877 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -10,7 +10,7 @@ ## 栈常用操作 -栈的常用操作见下表(方法命名以 Java 为例)。 +栈的常用操作见下表,方法名需根据语言来确定,此处以 Java 为例。
@@ -19,8 +19,6 @@ | push() | 元素入栈(添加至栈顶) | $O(1)$ | | pop() | 栈顶元素出栈 | $O(1)$ | | peek() | 访问栈顶元素 | $O(1)$ | -| size() | 获取栈的长度 | $O(1)$ | -| isEmpty() | 判断栈是否为空 | $O(1)$ |
@@ -30,25 +28,24 @@ ```java title="stack.java" /* 初始化栈 */ - // 在 Java 中,推荐将 ArrayList 当作栈来使用 - List stack = new ArrayList<>(); - + Stack stack = new Stack<>(); + /* 元素入栈 */ - stack.add(1); - stack.add(3); - stack.add(2); - stack.add(5); - stack.add(4); - + stack.push(1); + stack.push(3); + stack.push(2); + stack.push(5); + stack.push(4); + /* 访问栈顶元素 */ - int peek = stack.get(stack.size() - 1); - + int peek = stack.peek(); + /* 元素出栈 */ - int pop = stack.remove(stack.size() - 1); - + int pop = stack.pop(); + /* 获取栈的长度 */ int size = stack.size(); - + /* 判断是否为空 */ boolean isEmpty = stack.isEmpty(); ``` @@ -70,7 +67,7 @@ int top = stack.top(); /* 元素出栈 */ - stack.pop(); + stack.pop(); // 无返回值 /* 获取栈的长度 */ int size = stack.size();