SqStack.java
package test;
public class SqStack{
private Object[]stackElem;
private int top;
public SqStack(int maxSize)
{
top = 0;
stackElem = new Object[maxSize];
}
public void clear() //空栈状态
{
top = 0;
}
public int length() //栈中元素个数
{
return top;
}
public boolean isEmpty() //判栈空
{
if(top == 0)
return true;
else
return false;
}
public Object peek() //取栈顶元素位置
{
if(!isEmpty())
return stackElem[top-1];
else
return null;
}
//将元素x压入栈中
public void push(Object x)throws Exception
{
if(top == stackElem.length)
throw new Exception("栈已满");
else
stackElem[top++]=x;
}
//删除并返回栈顶元素
public Object pop()
{
if(isEmpty())
return null;
else
return stackElem[--top];
}
}
Demo8.java
package test;
import java.util.Scanner;
public class Demo8 {
/**
* @lijia
* 括号匹配问题