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條經驗 獲得超8個贊
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
提交
取消