import java.util.Stack;import java.util.NoSuchElementException;public class Queue extends Stack{public final int dump=1024;private Stack stk;public Queue( ){ /* 在此插入代码*/ }public boolean add(E e) throws IllegalStateException, ClassCastException, NullPointerException, IllegalArgumentException{ /* 在此插入代码*/ }public boolean offer(E e) throws ClassCastException, NullPointerException, IllegalArgumentException{ /* 在此插入代码*/ }public E remove( ) throws NoSuchElementException { /* 在此插入代码*/ }public E poll( ) { /* 在此插入代码*/ }public E peek ( ) { /* 在此插入代码*/ }public E element( ) throws NoSuchElementException { /* 在此插入代码*/ }}填充这几个函数
2 回答
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
public Queue( ){ stk = new stk(); } public boolean add(E e) throws IllegalStateException, ClassCastException, NullPointerException, IllegalArgumentException{ this.push(e); } public boolean offer(E e) throws ClassCastException, NullPointerException, IllegalArgumentException{ return this.size() > 0; } public E remove( ) throws NoSuchElementException { return this.pop(); } public E pull( ) { while ( E e = this.pop()) this.stk.push(e); E r = e; while ( E e =stk.pop()) this.push(e); return r; } public E peek ( ) { return this.peek(); } public E element( ) throws NoSuchElementException { throws new NoSuchElementException (); //这个不需要实现 }
扬帆大鱼
TA贡献1799条经验 获得超9个赞
public class Queue<E> extends Stack<E>{ public final int dump=1024; private Stack<E> stk; public Queue( ){ stk = new Stack<E>(); } public boolean add(E e) throws IllegalStateException, ClassCastException, NullPointerException, IllegalArgumentException{ stk.push(e); return true; } public boolean offer(E e) throws ClassCastException, NullPointerException, IllegalArgumentException { return this.add(e); } public E remove() throws NoSuchElementException { synchronized(this) { if (!this.isEmpty()) { return super.pop(); } else { while(!stk.isEmpty()) this.push(stk.pop()); return super.pop(); } } } public E poll( ) { return this.remove(); } public E peek ( ) { synchronized(this) { if (!this.isEmpty()) { return super.peek(); } else { while(!stk.isEmpty()) super.push(stk.pop()); return super.peek(); } } } public E element( ) throws NoSuchElementException { throws new NoSuchElementException (); //这个不需要实现 } }
添加回答
举报
0/150
提交
取消