为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

弑天下 2018-07-13 09:22:04
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 (); //这个不需要实现
}


查看完整回答
反对 回复 2018-08-04
?
扬帆大鱼

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 (); //这个不需要实现 }
}


查看完整回答
反对 回复 2018-08-04
  • 2 回答
  • 0 关注
  • 925 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信