数据结构--队的插入和删除
队:先进先出 类似生活中的火车(先进站的先出站)
队插入和删除的代码:
运用的接口
public interface Stack1 {
public void push(Object obj)throws Exception;
public Object tush()throws Exception;
public boolean isEmoty();
}
public class 队的入栈和出栈 implements Stack1{
String [] arr=new String[5];
int rear=-1; int front=0;
@Override
public void push(Object obj) throws Exception {
if(rear>=arr.length-1){
throw new Exception("队满了");
}else{
rear++;
arr[rear]=(String) obj;
System.out.println(obj+"入队了");
}
}
@Override
public Object tush() throws Exception {
Object object;
/*if(front>rear){
throw new Exception("异常");
}else{
object=arr[front];
arr[front]=null;
front++;
System.out.println(object+"出队了");
}
return arr[front];*/
arr[0]=arr[front];
object=arr[0];
arr[0]=null;
front++;
System.out.println(object+"出战了");
return arr[front];
}
@Override
public boolean isEmoty() {
return front>rear;
}
public static void main(String[] args) throws Exception {
队的入栈和出栈 s=new 队的入栈和出栈 ();
s.push("aaa");
s.push("bbb");
s.push("ccc");
s.push("ddd");
s.push("eee");
s.tush();
s.tush();
s.tush();
s.tush();
}
}