Directorio de artículos
Introducción
La pila es una estructura lineal con el primero en entrar, el último en salir.
Uno, almacenamiento de orden de pila [implementación de matriz]
La estructura de la pila
public class ArrayStack {
/**
* max size of the stack
*/
private int maxSize;
/**
* a stack object
*/
private Object[] stack;
/**
* init the top of the stack
* from 0 to maxSize
*/
private int top = -1;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new Object[this.maxSize];
}
------functions------
}
1, en la pila
Ideas:
1. Primero juzgue si la pila está llena y regrese si está llena
2. De lo contrario, agregue 1 a la parte superior y luego asigne un valor a la matriz de la pila.
Mira la foto y habla:
Código:
/**
* push a element
*
* @param object
*/
public void push(Object object) {
if (this.isFull()) {
System.out.println("the stack is full ! can't push !");
return;
}
stack[++top] = object;
}
2. Fuera de la pila
Ideas:
1. Primero juzgue si la pila está vacía, si está vacía, pregunte y regrese.
2. De lo contrario, primero guarde el nodo y luego reste 1 de la parte superior.
Mira la foto y habla:
Código:
/**
* pop a element
*
* @return
*/
public Object pop() {
if (isFull()) {
throw new RuntimeException("the stack is empty!");
}
return stack[top--];
}
3. Juzga que la pila está llena
Código:
/**
* Whether the stack is full
*
* @return
*/
public boolean isFull() {
return top > this.maxSize - 1;
}
4. Determine que la pila está vacía
Código:
/**
* Whether the stack is empty
*
* @return
*/
public boolean isEmpty() {
return top == -1;
}
5. Obtenga la longitud de la pila
Ideas:
Debido a que el puntero superior siempre apunta a la parte superior de la pila, el valor de top es la longitud de la pila, pero como este caso comienza desde 0, la longitud de la pila debe aumentarse en 1, es decir: top + 1.
Mira la foto y habla:
Código:
/**
* get length of stack
* @return
*/
public int getLength(){
return top+1;
}
6. Atraviesa la pila
Código:
/**
* print a stack
*/
public void list() {
if (isEmpty()) {
System.out.println("the stack is empty!");
}
for (int i = top; i >= 0; i--) {
System.out.print("stack[" + i + "]=");
System.out.println(stack[i]);
}
}
Código completo
package com.qingfeng.stack.array;
@SuppressWarnings("all")
public class ArrayStack {
/**
* max size of the stack
*/
private int maxSize;
/**
* a stack object
*/
private Object[] stack;
/**
* init the top of the stack
* from 0 to maxSize
*/
private int top = -1;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new Object[this.maxSize];
}
/**
* Whether the stack is empty
*
* @return
*/
public boolean isEmpty() {
return top == -1;
}
/**
* Whether the stack is full
*
* @return
*/
public boolean isFull() {
return top > this.maxSize - 1;
}
/**
* push a element
*
* @param object
*/
public void push(Object object) {
if (this.isFull()) {
System.out.println("the stack is full ! can't push !");
return;
}
stack[++top] = object;
}
/**
* pop a element
*
* @return
*/
public Object pop() {
if (isFull()) {
throw new RuntimeException("the stack is empty!");
}
return stack[top--];
}
/**
* print a stack
*/
public void list() {
if (isEmpty()) {
System.out.println("the stack is empty!");
}
for (int i = top; i >= 0; i--) {
System.out.print("stack[" + i + "]=");
System.out.println(stack[i]);
}
}
/**
* get length of stack
* @return
*/
public int getLength(){
return top+1;
}
}
prueba
Código:
public class ArrayStackTest {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(5);
/*------------------------------------------------------------------*/
System.out.println("-----testPush-----");
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(80);
stack.list();
/*------------------------------------------------------------------*/
System.out.println("-----testPop-----");
stack.pop();
stack.list();
/*------------------------------------------------------------------*/
System.out.println("-----testGetLength-----");
System.out.println("the length of stack is: "+stack.getLength());
}
}
resultado de la operación:
-----testPush-----
stack[3]=80
stack[2]=30
stack[1]=20
stack[0]=10
-----testPop-----
stack[2]=30
stack[1]=20
stack[0]=10
-----testGetLength-----
the length of stack is: 3
Process finished with exit code 0