JAVA泛型队列继承泛型栈要如何操作? 20C

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 { /* 在此插入代码*/ }
}
填充这几个函数

3个回答

 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 (); //这个不需要实现
}
 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 (); //这个不需要实现 }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐