//使用数组来实现栈
package 数组实现栈;
import java.util.Arrays;
interface StackADT {
public void push(Object element);//压栈
public Object pop();//出栈
public boolean isEmpty();//判断是否为空栈
public int size();
public Object peek();
public String toString();
}
public class Stack implements StackADT {
private final int DEFAULT_SIZE = 3; //默认数组大小
private int size = 0;
private int capacity = 0;
//top指向下一个能够添加元素的位置
private int top = 0;
private Object[] array;
public Stack() {//设置数组初始大小
this.capacity = this.DEFAULT_SIZE;
this.array = new Object[this.capacity];
this.size = 0;
}
public Stack(int capacity) { //控制数组容量
this.capacity = capacity;
this.array = new Object[this.capacity];
this.size = 0;
}
@Override
public void push(Object element) { //压栈
if (this.size < this.capacity) {
this.array[top] = element;
this.top++;
this.size++;
} else {
enlarge();
push(element);
}
}
public void enlarge() { //扩容
this.capacity = this.capacity + this.DEFAULT_SIZE;
Object[] newArray = new Object[this.capacity];
System.arraycopy(array, 0, newArray, 0, array.length);
Arrays.fill(array, null);
this.array = newArray;
}
@Override
public Object pop() { //查看栈顶元素并将栈顶元素删除
Object element = (Object) this.array[top - 1];
this.array[top - 1] = null;
this.size--;
return element;
}
@Override
public boolean isEmpty() { //判断栈是否为空
return size == 0;
}
@Override
public int size() { //返回栈的大小
return size;
}
@Override
public Object peek() { //查看栈顶元素但不删除
return (Object) this.array[this.top - 1];
}
public static void main(String[] args) {
Stack s = new Stack();
s.push(1);
s.push(2);
s.push(3);
System.out.println(s.pop());
System.out.println(s.size());
}
}
package 数组实现栈;
import java.util.Arrays;
interface StackADT {
public void push(Object element);//压栈
public Object pop();//出栈
public boolean isEmpty();//判断是否为空栈
public int size();
public Object peek();
public String toString();
}
public class Stack implements StackADT {
private final int DEFAULT_SIZE = 3; //默认数组大小
private int size = 0;
private int capacity = 0;
//top指向下一个能够添加元素的位置
private int top = 0;
private Object[] array;
public Stack() {//设置数组初始大小
this.capacity = this.DEFAULT_SIZE;
this.array = new Object[this.capacity];
this.size = 0;
}
public Stack(int capacity) { //控制数组容量
this.capacity = capacity;
this.array = new Object[this.capacity];
this.size = 0;
}
@Override
public void push(Object element) { //压栈
if (this.size < this.capacity) {
this.array[top] = element;
this.top++;
this.size++;
} else {
enlarge();
push(element);
}
}
public void enlarge() { //扩容
this.capacity = this.capacity + this.DEFAULT_SIZE;
Object[] newArray = new Object[this.capacity];
System.arraycopy(array, 0, newArray, 0, array.length);
Arrays.fill(array, null);
this.array = newArray;
}
@Override
public Object pop() { //查看栈顶元素并将栈顶元素删除
Object element = (Object) this.array[top - 1];
this.array[top - 1] = null;
this.size--;
return element;
}
@Override
public boolean isEmpty() { //判断栈是否为空
return size == 0;
}
@Override
public int size() { //返回栈的大小
return size;
}
@Override
public Object peek() { //查看栈顶元素但不删除
return (Object) this.array[this.top - 1];
}
public static void main(String[] args) {
Stack s = new Stack();
s.push(1);
s.push(2);
s.push(3);
System.out.println(s.pop());
System.out.println(s.size());
}
}