昨天自学到QUEUE CLASS,然后书上给出这个代码作为例子,可是发现看不懂....麻烦大家帮忙解释一下,谢谢!
class queue
{
char q[];
int putloc, get loc;
queue(int size)
{
q = new char[size+1];
putloc = getloc = 0;
}
void put(char ch)
{
if(putloc == q.length-1)
{
System.out.println("--queue is full");
return;
}
putloc++
q[putloc] = ch;
}
char get()
{
if(getloc == putloc)
{
System.out.println("--queue is empty");
return (char) 0;
}
getloc++;
return q[getloc];
}
}
class qdemo
{
public static void main(String args[])
{
queue bigq = new queue(100);
queue smallq = new queue(4);
char ch;
int i;
System.out.println("using bigq to store the alphabet");
for(i=0; i<26; i++)
bigq.put((char) ('A' + i));
System.out.print("contents of bigq ");
for(i=0; i<26; i++)
{
ch = bigq.get();
if(ch != (char) 0) System.out.print(ch);
}
System.out.println("\n");
System.out.println("using smallq to generate errors");
for(i=0; i<5; i++)
{
System.out.print("attempting to store " + (char) ('Z' - i));
smallq.put((char) ('Z' - i));
System.out.println();
}
System.out.println();
System.out.print("contents of smallq ");
for(i=0; i<5; i++)
{
ch = smallq.get();
if(ch != (char) 0) System.out.print(ch);
}
}
}
class queue
{
char q[];
int putloc, get loc;
queue(int size)
{
q = new char[size+1];
putloc = getloc = 0;
}
void put(char ch)
{
if(putloc == q.length-1)
{
System.out.println("--queue is full");
return;
}
putloc++
q[putloc] = ch;
}
char get()
{
if(getloc == putloc)
{
System.out.println("--queue is empty");
return (char) 0;
}
getloc++;
return q[getloc];
}
}
class qdemo
{
public static void main(String args[])
{
queue bigq = new queue(100);
queue smallq = new queue(4);
char ch;
int i;
System.out.println("using bigq to store the alphabet");
for(i=0; i<26; i++)
bigq.put((char) ('A' + i));
System.out.print("contents of bigq ");
for(i=0; i<26; i++)
{
ch = bigq.get();
if(ch != (char) 0) System.out.print(ch);
}
System.out.println("\n");
System.out.println("using smallq to generate errors");
for(i=0; i<5; i++)
{
System.out.print("attempting to store " + (char) ('Z' - i));
smallq.put((char) ('Z' - i));
System.out.println();
}
System.out.println();
System.out.print("contents of smallq ");
for(i=0; i<5; i++)
{
ch = smallq.get();
if(ch != (char) 0) System.out.print(ch);
}
}
}
比如说 char q[]; q后面为什么会加一个[]。
还有 void put(char ch)
{
if(putloc == q.length-1)
{
System.out.println("--queue is full");
return;
}
---------
代表是一个字符数组.void put(char ch)
{
if(putloc == q.length-1)
{
System.out.println("--queue is full");
return;
} 将一个字符压入栈,如果已经超过规定长度,就输出队列己满的提示.
{
char q[];
int putloc, getloc;
queue(int size)
{
q = new char[size+1];
putloc = getloc = 0;
}
void put(char ch)
{
if(putloc == q.length-1) //如果putloc指向q[]的最后一个位置
{
System.out.println("--queue is full");
return; //打印--queue is full,并返回不往q[]中添加字符
}
putloc++;//存放位置向前移动,指向q[]所存储数据的位置
q[putloc] = ch; //存储数据
}
char get()
{
if(getloc == putloc) //如果取到的数据的位置是最后一个字符的位置
{
System.out.println("--queue is empty");
return (char) 0; //打印--queue is empty,并返回为0的字符
}
getloc++; //取出位置向前移动
return q[getloc]; //返回取出的当前位置
} }
class qdemo
{
public static void main(String args[])
{
queue bigq = new queue(100); //实例queue创建100个长度的char q[];
queue smallq = new queue(4); //实例queue创建4个长度的char q[];
char ch;
int i;
System.out.println("using bigq to store the alphabet");
for(i=0; i <26; i++)
bigq.put((char) ('A' + i)); //存放位置
System.out.print("contents of bigq ");
for(i=0; i <26; i++)
{
ch = bigq.get(); //取出位置
if(ch != (char) 0) System.out.print(ch);//如果不是最后一个位置就返回取出的位置
}
System.out.println("\n");
System.out.println("using smallq to generate errors");
for(i=0; i <5; i++)
{
System.out.print("attempting to store " + (char) ('Z' - i));
smallq.put((char) ('Z' - i));
System.out.println();
}
System.out.println();
System.out.print("contents of smallq ");
for(i=0; i <5; i++)
{
ch = smallq.get();
if(ch != (char) 0) System.out.print(ch);
}
}
}
比如 q.length - 1, 这个要怎么理解呢
你刚刚接触编成把,多看些资料,别什么都问别人
.length是数组的长度, -1是指向数组中存储的最后一个数据 因为数组下标是从0开始的