Java ToyProgam LunacyDoctor implements IO and Sequence Stack
2007-06-18 10:57
661 查看
前面写了链栈 http://blog.csdn.net/nyzhl/archive/2007/06/18/1656153.aspx
现在写了个程序实现顺序栈
//Coding by zhaohongliang
import java.io.*;
public class LunacyDoctor ...{
public static void main(String[] args) ...{
BufferedReader sr =new BufferedReader(
new InputStreamReader (System.in) );
String inString = null;
String[] temp = null;
try ...{
inString = sr.readLine();
while(inString!=null) ...{
if(inString.equalsIgnoreCase("exit")) ...{
System.exit(-1);
}
temp = inString.split(" ");
SeqStack<String> ss = new SeqStack<String>(temp.length);
for(String s : temp) ...{
ss.push(s);
}
while(!ss.empty()) ...{
System.out.print(ss.pop()+" ");
}
System.out.println();
inString=sr.readLine();
}
sr.close();
}
catch(IOException e1) ...{
System.out.println("Error:IO Error.");
}
catch(EmptyStackException e2) ...{
System.out.println("Error:Popped an empty Stack.");
}
catch(StackOverFlowException e3) ...{
System.out.println("Error:Stack over flow.");
}
}
}
class SeqStack<T> ...{
private final int size;
private T[] elem;
private int top;
public SeqStack(int size) ...{
this.top = -1;
this.size = size;
elem = (T[])new Object[size];
//elem = new T[size]; 此句JDK会报错:创建泛型数组
}
public boolean empty() ...{
return top==-1 ? true : false ;
}
public boolean full() ...{
return elem.length-1==top ? true : false;
}
public void push(T item) ...{
if(full()) throw new StackOverFlowException();
top ++ ;
elem[top] = item;
}
public T pop() ...{
if(empty()) throw new EmptyStackException();
top -- ;
return elem[top+1];
}
public T peek() ...{
if(empty()) throw new EmptyStackException();
return elem[top];
}
}
class StackOverFlowException extends RuntimeException ...{}
class EmptyStackException extends RuntimeException ...{}
//在java.util包里也有EmptyStackException
可以看出,顺序栈是基于数组的,数组声明时必须声明长度。程序可扩展性差。而且数组的地址空间是固定的。如果强行重新构造数组(ArrayList)将造成写操作执行效率低下。但是数组的地址是可计算的,所以在读写时的效率很高。
而链栈是基于链表的,只要你内存够,就可以无限扩展。但是在读写操作时,要找到目标对象比较耗费资源。但是栈只是针对栈顶操作,就不存在这个问题了。所以我觉得综合来看,还是链栈比较好用。不知道JRE里的栈是怎么实现的。
现在写了个程序实现顺序栈
//Coding by zhaohongliang
import java.io.*;
public class LunacyDoctor ...{
public static void main(String[] args) ...{
BufferedReader sr =new BufferedReader(
new InputStreamReader (System.in) );
String inString = null;
String[] temp = null;
try ...{
inString = sr.readLine();
while(inString!=null) ...{
if(inString.equalsIgnoreCase("exit")) ...{
System.exit(-1);
}
temp = inString.split(" ");
SeqStack<String> ss = new SeqStack<String>(temp.length);
for(String s : temp) ...{
ss.push(s);
}
while(!ss.empty()) ...{
System.out.print(ss.pop()+" ");
}
System.out.println();
inString=sr.readLine();
}
sr.close();
}
catch(IOException e1) ...{
System.out.println("Error:IO Error.");
}
catch(EmptyStackException e2) ...{
System.out.println("Error:Popped an empty Stack.");
}
catch(StackOverFlowException e3) ...{
System.out.println("Error:Stack over flow.");
}
}
}
class SeqStack<T> ...{
private final int size;
private T[] elem;
private int top;
public SeqStack(int size) ...{
this.top = -1;
this.size = size;
elem = (T[])new Object[size];
//elem = new T[size]; 此句JDK会报错:创建泛型数组
}
public boolean empty() ...{
return top==-1 ? true : false ;
}
public boolean full() ...{
return elem.length-1==top ? true : false;
}
public void push(T item) ...{
if(full()) throw new StackOverFlowException();
top ++ ;
elem[top] = item;
}
public T pop() ...{
if(empty()) throw new EmptyStackException();
top -- ;
return elem[top+1];
}
public T peek() ...{
if(empty()) throw new EmptyStackException();
return elem[top];
}
}
class StackOverFlowException extends RuntimeException ...{}
class EmptyStackException extends RuntimeException ...{}
//在java.util包里也有EmptyStackException
可以看出,顺序栈是基于数组的,数组声明时必须声明长度。程序可扩展性差。而且数组的地址空间是固定的。如果强行重新构造数组(ArrayList)将造成写操作执行效率低下。但是数组的地址是可计算的,所以在读写时的效率很高。
而链栈是基于链表的,只要你内存够,就可以无限扩展。但是在读写操作时,要找到目标对象比较耗费资源。但是栈只是针对栈顶操作,就不存在这个问题了。所以我觉得综合来看,还是链栈比较好用。不知道JRE里的栈是怎么实现的。
相关文章推荐
- Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.
- Cause: java.io.UTFDataFormatException: Invalid byte 3 of 3-byte UTF-8 sequence. 解决办法
- Java IO: System.in, System.out, and System.error
- nutch java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence
- java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=169869568)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)
- public class 实体类名 implements java.io.Serializable{...}
- Java Basic4(Exception and Java IO)
- Java_IO_字节流_Reader_and_Writer
- Can not find a java.io.InputStream with the name [] in the invocation stack 解决办法
- Java step by step(2): Hibernate and Oracle Sequence
- Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack问题解决
- Difference between Stack and Heap memory in Java
- PI java stack Archiving and Delete
- struts2文件下载出现Can not find a java.io.InputStream with the name [imageStream] in the invocation stack.
- Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <
- Java_IO_BufferedIntputStream_And_BufferedOutputStream
- (文件下载)Can not find a java.io.InputStream with the name [inputStream] in the invocation stack
- About stack and heap in JAVA(3)
- Java IO reader and writer
- Android5.0以上使用XUtils访问网络出现错误或者网络数据无法加载: java.io.IOException: stack size 1036KB