首页 > 试题广场 >

请实现入栈和出栈的操作,规格如下: class Stack

[问答题]
请实现入栈和出栈的操作,规格如下:
class Stack {
    public:
        Stack();
        voidpush(string value);
        stringpop();//throw
        PopEmptyStackException if stack is empty
}

//用map的大小作为key
class Stack
{
private Hashtable <KeyT,ValueT> table;
Stack()
{
table=new Hashtable<KeyT,ValueT>();
}
public void push(ValueT x)
{
table.set(table.size()+1,x);
}
public ValueT pop()
{
if(table.size()==0) return  -1;
else
{
ValueT top=table.get(table.size());
table.delete(table.size());
return top;
}
}
}
发表于 2017-10-13 22:09:41 回复(0)
class Stack{
    List<String> datas = new ArrayList<>();
    
    int top = -1;
    
    void push(String val) {
        datas.add(0,val);
    }
    
    String pop() {
        if(datas.size() == 0) {
            throw new RunTimeException("栈空");
        }
        String pop = datas.get(0);
        datas.remove(0);
        return pop;
    }
}

发表于 2019-09-29 16:29:47 回复(0)
class Stack {
    private int top=-1;
    private  String[] array=new String[MAX_LEN];
    public:
        Stack();
        void push(String value)throws StackOverFlowException{
            int index=++top;
            if(index>=MAX_LEN){
                throw New StackOverFlowException();
                return ;
            }
            array[index]=value;
        }
        String pop()throws PopEmptyStackException{
            if(top==-1){
                throw new PopEmptyStackException();
                return null;
            }
            return array[top--];    
        }
}
发表于 2019-03-05 17:03:21 回复(0)