1. Implementar una pila basada en una matriz.
- Comprobar si la pila está vacía
- elemento superior
- pila
- salir
- Expansión (porque es una matriz, debe expandirse)
1. Definir variables
Se requiere una matriz y un puntero para apuntar al espacio anterior del elemento superior de la pila (también puede apuntar al elemento superior de la pila, según los requisitos)
private Object[] stack;
private int top;
2. Inicialización
MyStack() {
stack = new Object[0];
}
3. El juicio es vacío
public boolean isEmpty() {
return top == 0;
}
4. Expansión
public void expandCapacity(int size) {
int length = stack.length;
if (size>length){
// 扩容两倍
size = size*3/2+1;
stack = Arrays.copyOf(stack,size);
}
}
5. El elemento superior de la pila.
public T peek() {
T t = null;
if (top > 0) t = (T) stack[top - 1];
return t;
}
6. Agrega elementos
public void push(T t) {
expandCapacity(top+1);
stack[top] = t;
top++;
}
7. Pop
public T pop() {
T t = peek();
if (top > 0) {
stack[top - 1] = null;
top--;
}
return t;
}
1.7 Código general
public class MyStack<T> {
private Object[] stack;
private int top;
// 初始化
MyStack() {
stack = new Object[0];
}
// 判断为空
public boolean isEmpty() {
return top == 0;
}
// 栈顶元素
public T peek() {
T t = null;
if (top > 0) t = (T) stack[top - 1];
return t;
}
// 添加元素
public void push(T t) {
expandCapacity(top + 1);
stack[top] = t;
top++;
}
// 出栈
public T pop() {
T t = peek();
if (top > 0) {
stack[top - 1] = null;
top--;
}
return t;
}
// 扩充容量
public void expandCapacity(int size) {
int length = stack.length;
if (size > length) {
// 扩容两倍
size = size * 3 / 2 + 1;
stack = Arrays.copyOf(stack, size);
}
}
}
2. La lista enlazada representa la pila
- Nodo de lista enlazada
- Nodo cabeza de nodo
- Juzgar que la pila está vacía
- el elemento superior de la pila
- salir
- pila
1. Nodo y nodo principal
class Node<T> {
public T t;
public Node next;
}
public Node<T> head;
2. Inicialización
ListStack() {
head = null;
}
3. Determinar si la pila está vacía
public boolean isEmpty() {
return head == null;
}
4. El elemento superior de la pila.
public T peek() {
if (head == null) {
return null;
}
return head.t;
}
2.5 estallar
public T pop() {
if (head == null) {
return null;
}
T t = head.t;
head = head.next;
return t;
}
6. Empuje
public void push(T t) {
if (t == null) {
throw new NullPointerException("null");
}
if (head == null) {
head = new Node<>();
head.t = t;
head.next = null;
} else {
Node<T> temp = head;
head = new Node<>();
head.t = t;
head.next = temp;
}
}
7. Código general
public class ListStack<T> {
class Node<T> {
public T t;
public Node next;
}
public Node<T> head;
ListStack() {
head = null;
}
public void push(T t) {
if (t == null) {
throw new NullPointerException("null");
}
if (head == null) {
head = new Node<>();
head.t = t;
head.next = null;
} else {
Node<T> temp = head;
head = new Node<>();
head.t = t;
head.next = temp;
}
}
public T pop() {
if (head == null) {
return null;
}
T t = head.t;
head = head.next;
return t;
}
public T peek() {
if (head == null) {
return null;
}
return head.t;
}
public boolean isEmpty() {
return head == null;
}
}