高手请进了啊!!急急急~~~~~
loyer 2006-11-18 11:59:09 import java.io.*;
import java.util.*;
class StackNode
{
int stackData = 0; //序列中的整数
StackNode prev = null; //前引用
StackNode next = null; //后引用
StackNode(int input) {stackData = input;}
}
class LinkStack
{
StackNode top;
public LinkStack(){top=null;}//构造
public void push(int k)//入栈
{
StackNode q=new StackNode(k);
if(top==null)
top=q;
else{
q.next=top;
top.prev = q;
top=top.prev;
}
}
public int pop()//出栈
{
int k=-1;
if(top!=null){k=top.stackData;top=top.next;}
return k;
}
public int get()// 取栈顶
{
if(top!=null) return top.stackData;
else return -1;
}
public void judgeSequence(LinkStack stack0,int l)
{
LinkStack stack1=new LinkStack();
LinkStack stack2=new LinkStack();
int i=0;
while(i<l)
{
int follow=stack0.pop();
while(stack1.get()!=-1||stack0.get()==-1){stack2.push(follow);i++;}
if(follow<stack0.get()) stack1.push(follow);
else
{
if(follow>stack1.get())
{
stack2.push(follow);i++;
}
else
{
int m=stack1.pop();
while(stack0.get()!=-1||stack1.get()==-1){int h=stack0.pop();stack2.push(h);i++;}
stack2.push(m);i++;stack1.push(follow);
}
}
}
for(i=0;i<l;i++)
{
int n=stack2.pop();
if(n!=stack2.get()-1){System.out.println("您输入的是非法序列");return;}
else System.out.println("您输入的是合法序列");
}
}
}
public class StackSequence
{
public static void main(String args[]) throws Exception{
{
while(true)
{
LinkStack stack0=new LinkStack();
String s;
System.out.println("请输入一个待判断的输出序列:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = br.readLine(); //读取一行输入的数据
if(s==null||s.length()==0) System.out.println("字符串长度应该大于或等于1");
else
{
if(s.equals("finish")==true) return;
else
{
char a[]=s.toCharArray();
for(int i=0;i<a.length;i++)
stack0.push(a[i]);
stack0.judgeSequence(stack0,a.length);
}
}
}
}
}
}
怎么编译没问题,但是运行时就提示内存溢出,找不到错误,请高手帮帮我啊!!