高手请进了啊!!急急急~~~~~

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);
}
}
}
}
}
}
怎么编译没问题,但是运行时就提示内存溢出,找不到错误,请高手帮帮我啊!!
...全文
178 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
loyer 2006-11-20
  • 打赏
  • 举报
回复
那该发在那种地方啊?
LoveCJ 2006-11-20
  • 打赏
  • 举报
回复
帮顶.
hm8328 2006-11-20
  • 打赏
  • 举报
回复
这种问题怎么发在这个板块?
帮忙UP一下
JafyLiu 2006-11-19
  • 打赏
  • 举报
回复
帮顶的

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧