mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-30 19:02:46 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -8,21 +8,21 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Stack based on array implementation */
|
||||
/* Stack implemented with an array */
|
||||
class ArrayStack {
|
||||
private ArrayList<Integer> stack;
|
||||
|
||||
public ArrayStack() {
|
||||
// Initialize list (dynamic array)
|
||||
// Initialize the list (dynamic array)
|
||||
stack = new ArrayList<>();
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
public int size() {
|
||||
return stack.size();
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -39,14 +39,14 @@ class ArrayStack {
|
||||
return stack.remove(size() - 1);
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stack.get(size() - 1);
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
/* Convert the list to an array and return it */
|
||||
public Object[] toArray() {
|
||||
return stack.toArray();
|
||||
}
|
||||
@@ -54,10 +54,10 @@ class ArrayStack {
|
||||
|
||||
public class array_stack {
|
||||
public static void main(String[] args) {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
ArrayStack stack = new ArrayStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
@@ -65,20 +65,20 @@ public class array_stack {
|
||||
stack.push(4);
|
||||
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int peek = stack.peek();
|
||||
System.out.println("Stack top element peek = " + peek);
|
||||
System.out.println("Top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
int pop = stack.pop();
|
||||
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + Arrays.toString(stack.toArray()));
|
||||
System.out.println("Popped element pop = " + pop + ", after popping, stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack.size();
|
||||
System.out.println("Stack length size = " + size);
|
||||
System.out.println("Stack size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("Stack is empty = " + isEmpty);
|
||||
System.out.println("Is the stack empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user