mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 08:34:28 +00:00
2778a6f9c7
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
85 lines
2.0 KiB
Java
85 lines
2.0 KiB
Java
/**
|
|
* File: array_stack.java
|
|
* Created Time: 2022-11-25
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
package chapter_stack_and_queue;
|
|
|
|
import java.util.*;
|
|
|
|
/* Stack based on array implementation */
|
|
class ArrayStack {
|
|
private ArrayList<Integer> stack;
|
|
|
|
public ArrayStack() {
|
|
// Initialize list (dynamic array)
|
|
stack = new ArrayList<>();
|
|
}
|
|
|
|
/* Get the length of the stack */
|
|
public int size() {
|
|
return stack.size();
|
|
}
|
|
|
|
/* Check if the stack is empty */
|
|
public boolean isEmpty() {
|
|
return size() == 0;
|
|
}
|
|
|
|
/* Push */
|
|
public void push(int num) {
|
|
stack.add(num);
|
|
}
|
|
|
|
/* Pop */
|
|
public int pop() {
|
|
if (isEmpty())
|
|
throw new IndexOutOfBoundsException();
|
|
return stack.remove(size() - 1);
|
|
}
|
|
|
|
/* Return list for printing */
|
|
public int peek() {
|
|
if (isEmpty())
|
|
throw new IndexOutOfBoundsException();
|
|
return stack.get(size() - 1);
|
|
}
|
|
|
|
/* Convert List to Array and return */
|
|
public Object[] toArray() {
|
|
return stack.toArray();
|
|
}
|
|
}
|
|
|
|
public class array_stack {
|
|
public static void main(String[] args) {
|
|
/* Access top of the stack element */
|
|
ArrayStack stack = new ArrayStack();
|
|
|
|
/* Elements push onto stack */
|
|
stack.push(1);
|
|
stack.push(3);
|
|
stack.push(2);
|
|
stack.push(5);
|
|
stack.push(4);
|
|
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
|
|
|
|
/* Return list for printing */
|
|
int peek = stack.peek();
|
|
System.out.println("Stack top element peek = " + peek);
|
|
|
|
/* Element pop from stack */
|
|
int pop = stack.pop();
|
|
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + Arrays.toString(stack.toArray()));
|
|
|
|
/* Get the length of the stack */
|
|
int size = stack.size();
|
|
System.out.println("Stack length size = " + size);
|
|
|
|
/* Check if empty */
|
|
boolean isEmpty = stack.isEmpty();
|
|
System.out.println("Stack is empty = " + isEmpty);
|
|
}
|
|
}
|