public class MyStack {
private int[] array;
private int maxSize;
private int top;
public MyStack(int size) {
this.maxSize = size;
array = new int[size];
top = -1;
}
//入栈
public void push(int value) {
if(top<maxSize-1) {
array[++top] = value;
}
}
//弹出栈顶数据
public int pop() {
return array[top--];
}
//访问栈顶数据
public int peek() {
return array[top];
}
//判断栈是否为空
public boolean isEmpty() {
return (top==-1);
}
//判断栈是否满了
public boolean isFull() {
return (top==maxSize-1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyStack st = new MyStack(3);
st.push(1);
st.push(2);
st.push(3);
System.out.println(st.peek());
while(!st.isEmpty()) {
System.out.println(st.pop());
}
}
}
//
增强栈的功能 1、自动扩容 2、可以存储各种不同类型的数据
import java.util.Arrays;
import java.util.EmptyStackException;
public class ArrayStack {
private Object[] elemenData;
private int top;
private int size;
//创建一个初始大小为10的栈
public ArrayStack(){
this.elemenData = new Object[10];
this.top = -1;
this.size = 10;
}
public ArrayStack(int initialCapacity) {
if(initialCapacity<0) {
throw new IllegalArgumentException("栈初始容量不能小于"+initialCapacity);
}
this.elemenData = new Object[initialCapacity];
this.top = -1;
this.size = initialCapacity;
}
//向栈中放入元素\
public Object push(Object item) {
isGrow(top+1);
elemenData[++top] = item;
return item;
}
//弹出栈顶元素
public Object pop() {
Object obj = peek();
remove(top);
return obj;
}
//获取栈顶元素
public Object peek() {
if(top==-1) {
throw new EmptyStackException();
}
return elemenData[top];
}
//判断栈是否为空
public boolean isEmpty() {
return (top==-1);
}
//删除栈顶元素
public void remove(int top) {
elemenData[top]=null;
this.top--;
}
public boolean isGrow(int minCapacity) {
int oldCapacity = size;
if(minCapacity>=oldCapacity) {
int newCapacity = 0;
if((oldCapacity<<1)-Integer.MAX_VALUE>0) {
newCapacity = Integer.MAX_VALUE;
}else {
newCapacity = (oldCapacity<<1);//左移一位,相当于放大2倍
}
size = newCapacity;
int [] newArray = new int[size];
elemenData = Arrays.copyOf(elemenData, size);
return true;
}else {
return false;
}
}
//利用栈实现字符串的逆序
public static void testStringReversal() {
ArrayStack stack = new ArrayStack();
String str ="how are you";
char [] ch =str.toCharArray();
for(char c:ch) {
stack.push(c);
}
while(!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
//分隔符匹配
public static void testMatch() {
ArrayStack stack = new ArrayStack(3);
String str = "12<a[b{c}]>";
char [] ch = str.toCharArray();
for(char c:ch) {
switch(c) {
case'{':
case'[':
case'<':
stack.push(c);
break;
case'}':
case']':
case'>':
if(!stack.isEmpty()) {
char cha = stack.pop().toString().toCharArray()[0];
if(c=='}'&&cha!='{' || c==']'&&cha!='[' || c=='>'&&cha!='<') {
System.out.println("Error:"+ch+"-"+c);
}
}
break;
default:
break;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// ArrayStack stack = new ArrayStack(3);
// stack.push(1);
// stack.push(2);
// stack.push(3);
// stack.push("abc");
// System.out.println(stack.peek());
// stack.pop();
// stack.pop();
// stack.pop();
// System.out.println(stack.peek());
//testStringReversal();
testMatch();
}
}