能编译,无法执行。请各位帮忙改一下
一个小程序,用来创建队列接口的,能编译,但是无法执行,麻烦各位修改一下。public interface ICharQ{
void put(char ch);
char get();
}
class FixedQueue implements ICharQ{
private char q[];
private int putloc,getloc;
public FixedQueue(int size){
q=new char;
putloc=getloc=0;
}
public void put(char ch){
if(putloc==q.length-1) {System.out.println("Queue is full");
return;
}
putloc++;
q=ch;
}
public char get(){
if(putloc==getloc) {System.out.println("Queue is empty.");
return (char) 0;
}
getloc++;
return q;
}
}
class CircularQueue implements ICharQ{
private char q[];
private int putloc,getloc;
public CircularQueue(int size){
q=new char;
putloc=getloc=0;
}
public void put(char ch){
if(putloc+1==getloc|((putloc==q.length-1)&(getloc==0))){
System.out.println("Queue is full.");
return;
}
putloc++;
if(putloc==q.length) putloc=0;
q=ch;
}
public char get(){
if(getloc==putloc) {System.out.println("Queue is empty.");
return (char)0;
}
getloc++;
if(getloc==q.length) getloc=0;
return q;
}
}
class DynQueue implements ICharQ{
private char q[];
private int putloc,getloc;
public DynQueue(int size){
q=new char;
putloc=getloc=0;
}
public void put(char ch){
if(putloc==q.length-1){
char t[]=new char;
for(int i=0;i<q.length;i++)
t=q;
q=t;
}
putloc++;
q=ch;
}
public char get(){
if(getloc==putloc){
System.out.println("Queue is empty.");
return (char) 0;
}
getloc++;
return q;
}
}
class IQDemo{
public static void main(String args[]){
FixedQueue q1=new FixedQueue(10);
DynQueue q2=new DynQueue(5);
CircularQueue q3=new CircularQueue(10);
ICharQ iQ;
char ch;
int i;
iQ=q1;
for(i=0;i<10;i++){
iQ.put((char) ('A'+i));
}
System.out.print("Contents of fixed queue.");
for(i=0;i<10;i++){
ch=iQ.get();
System.out.print(ch);
}
System.out.println();
iQ=q2;
for(i=0;i<10;i++)
iQ.put((char)('Z'-i));
System.out.print("Contents of dynamic queue:");
for(i=0;i<10;i++){
ch=iQ.get();
System.out.print(ch);
}
System.out.println();
iQ=q3;
for(i=0;i<10;i++){
iQ.put((char)('A'+i));
}
System.out.print("Contents of circular queue:");
for(i=0;i<10;i++){
ch=iQ.get();
System.out.print(ch);
}
System.out.println();
for(i=10;i<20;i++)
iQ.put((char)('A'+i));
System.out.print("contents of circular queue:" );
for(i=0;i<10;i++){
ch=iQ.get();
System.out.print(ch);
}
System.out.println();
System.out.print("store and consume from" + "circular queue");
for(i=0;i<20;i++){
iQ.put((char)('A'+i));
ch=iQ.get();
System.out.print(ch);
}
}
}
谢谢! sourcecode没贴对。重贴。 应该是对的吧,我又用Jcreator编译了一编,这回又可以运行了。奇怪!是不是设置的问题呀 谢谢文选流氓帮我的编辑。现在看起来好多了 呵呵,有点奇怪,你怎么总是和队列打交道。你的程序没有问题,只是开始的Inteface应该单独放到一个JAVA文件里。
页:
[1]