您的位置:首页 > 编程语言

队列(顺序存储)代码

2016-04-29 08:57 155 查看
publicclassArrayQueue<T>{




privateT[]queue;


privatestaticintsize=20;//队列容量


privateintfront,rear;//队首、队尾下标




publicArrayQueue(){


queue=(T[])newObject[size];


front=0;


rear=0;


}




publicvoidadd(Tt)throwsException


{


if((rear+1)%size==front)


thrownewException("overflow!");




rear=(rear+1)%size;


queue[rear]=t;


}




publicTpoll()throwsException


{


if(front==rear)


thrownewException("underflow!");




front=(front+1)%size;


returnqueue[front];


}




publicbooleanisEmpty(){


returnfront==rear;


}




}


publicclassTest{




publicstaticvoidmain(String[]args)throwsException{




ArrayQueue<String>q=newArrayQueue<String>();


q.add("a");


q.add("b");


q.add("c");


q.add("d");


q.add("e");


q.add("f");


q.add("g");


q.add("h");


q.add("i");


q.add("j");


q.add("k");


q.add("l");


q.add("m");


while(!q.isEmpty()){


Stringtemp=q.poll();


System.out.print(temp);


}


}




}


输出

abcdefghijklm


来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: