数据结构JAVA语言描述第三章上机练习题.doc
文本预览下载声明
3-16
public interface back{
public void append(Object obj) throws Exception;
public Object delete() throw Exception;
public Object getFront() throw Exception;
public boolean notEmpty();
}
public class Forback implements back {
static final int defauleSize = 10;
int front;
int rear;
int count;
int maxSize;
Object[] data;
int tag=0;
public Forback() {
initiate(defauleSize);
}
public Forback(int sz) {
initiate(sz);
}
private void initiate(int sz) {
maxSize = sz;
front = rear = 0;
count = 0;
data = new Object[sz];
}
public void append(Object obj) throws Exception {
if (count 0 front == rear) {
throw new Exception(出错了);
}
data[rear]=obj;
rear=(rear+1)%maxSize;
count++;
tag=1;
}
public Object delete() throws Exception {
if(count==0){
throw new Exception(队列已空);
}
Object temp=data[front];
front=(front+1)%maxSize;
count--;
tag=0;
return temp;
}
public Object getFront() throws Exception {
if(count==0){
throw new UnsupportedOperationException(队列已空);
}
return data[front];
}
public boolean notEmpty() {
return count!=0;
}
}
public class Text_for_16 {
public static void main(String[]args){
int n=10;
SeQueue m=new SeQueue(n);
int temp=11;
try{
for(int i=1;i=n;i++){
m.append(i);
}
System.out.println();
System.out.println( 取值: +m.getFront());
System.out.println(删除: +m.delete());
System.out.println(所得结果为 +temp);
m.append(temp);
System.out.print(m.getFront()+ );
if(m.rear==m.frontm.tag==0){
System.out.println();
}
else
{
Syst
显示全部