获取栈中任意位置的元素

递归获取栈中指定位置的元素并删除,可以不破坏栈的结构

public int getElement(Stack stack, int position)
    {
        int result = stack.pop();
        if (stack.size() == position)
        {
//            stack.push(result);
            return result;
        }else {
            int element = getElement(stack, position);
            stack.push(element);
            return element;
        }
    }

你可能感兴趣的:(数据结构与算法,算法与数据结构)