Java数据结构(习题)

1.给定两个序列 第一个序列是栈的压入序列 第二个序列是栈的弹出序列 判断第二个序列是否是第一个序列正确的弹出序列

  • 问题分析:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入序列为1,2,3,4,5,其可能的一个弹出顺序为4,5,3,2,1(先入1,2,3,再入4,随后弹出4,再入5随后全部弹出)。但4,3,5,1,2(若要4先出,则应4再3前入栈)就不可能为此栈的弹出队列。
  • 思路分析:我们可以借助一个栈,同时再两个队列中分别定义两个临时指针。首先,让入栈队列中的元素按临时指针依次入栈,随后获取栈顶元素与出栈队列中的临时指针进行比较,相等时便弹出并让出栈队列中的临时指针往后移动,继续循环,若临时指针与栈顶元素不相等,则让入栈队列元素依次继续入栈。
  • 代码实现:

public static boolean isPosArray(int[] array1,int[] array2){
    
    
        if(array1 == null || array1.length == 0 || array2 == null || array2.length == 0){
    
    
            return false;
        }
        //array1中的序列依次入栈,array2定义一个临时指针,从头往后走
        //临时指针在array2中的元素与栈顶元素不相等的时候,继续入栈
        Stack<Integer> stack = new Stack<>();
        int j = 0; //表示array2中的临时指针
        for(int i=0; i<array1.length; i++){
    
    
            //i表示当前array1中的临时指针
            int curName = array1[i];
            stack.push(curName);
            while(!stack.isEmpty() && stack.peek() == array2[j]){
    
    
                stack.pop();
                j++;
            }
        }
        if(stack.isEmpty()){
    
    
            return true;
        }
        System.out.println("Given array is not a pop-stach array");
        return false;
    }

2.在栈中实现一个函数,返回栈中最小元素

  • 思路分析:要实现一个能返回栈中最小元素的功能,我们可以借助一个辅助栈来实现。可以让我们的辅助栈一直保存数据栈中的最小元素,这样一来在获取最小元素时可以直接获取辅助栈的栈顶元素。那么在入栈时就要将入数据栈元素与辅助栈栈顶元素进行比较,若比栈顶元素小,则将其入栈后,将辅助栈栈顶元素也更新为次元素。
  • 代码实现:
    public static Stack<Integer> dataStack = new Stack<>();
    public static Stack<Integer> assistStack = new Stack<>();
    public static int getMin(){
    
    
        if(assistStack.isEmpty()){
    
    
            throw new UnsupportedOperationException("the stack has been empty");
        }
        return assistStack.peek();
    }

    public static void pushStack(int value){
    
    
        dataStack.push(value);

        if(assistStack.isEmpty() || value < assistStack.peek()){
    
    
            assistStack.push(value);
        }else{
    
    
            assistStack.push(assistStack.peek());
        }
    }

    public static int popStack(){
    
    
        if(dataStack.isEmpty()){
    
    
            throw new UnsupportedOperationException("the stack has been empty");
        }
        assistStack.pop();
        return dataStack.pop();
    }

3.两个队列实现一个栈

  • 思路分析:两个队列实现一个栈,即用两个表实现一个栈的功能,即先进后出,我们就需要实现这个栈的入栈和出栈方法。在入栈时,若一个队列为空,则将元素入另一个队列。始终保持两个队列中有一个队列有数据。在弹出时,要实现栈先进先出的特性,则每次弹出时把队列中的元素放入另一个队列中,删除最后一个元素。
  • 代码实现:
    3
    public static Queue<Integer> queue1 = new LinkedList<>();
    public static Queue<Integer> queue2 = new LinkedList<>();
    public static void pushStack(int value){
    
    
        if(queue1.isEmpty() && queue2.isEmpty()){
    
    
            queue1.add(value);
        }else if(queue1.isEmpty()){
    
    
            queue2.add(value);
        }else{
    
    
            queue1.add(value);
        }
    }

    public static int popStack(){
    
    
        //popQueue是当前的数据队列
        // 1 ? 2 : 3  1为true 选择2作为结果 反之选择3作为结果
        Queue<Integer> popQueue = queue1.isEmpty() ? queue2 : queue1;
        //pushQueue是当前的辅助队列
        Queue<Integer> pushQueue = queue1.isEmpty() ? queue1 : queue2;

        while(popQueue.size() > 1){
    
    
            pushQueue.add(popQueue.remove());
        }
        return popQueue.remove();
    }

4.两个栈实现一个队列

  • 思路分析:与第三题类似,入队时可直接在stack1中入栈,出栈时,若stack2为空,则当stack1不为空时,让stack2入stack1的栈顶元素,最后返回stack2的栈顶元素。
public static Stack<Integer> stack1 = new Stack<>();
    public static Stack<Integer> stack2 = new Stack<>();
    public static void pushQueue(int value){
    
    
        stack1.push(value);
    }

    public static int popQueue(){
    
    
        if(stack1.isEmpty() && stack2.isEmpty()){
    
    
            throw new UnsupportedOperationException("the stack has been empty");
        }
        if(stack2.isEmpty()){
    
    
            while(!stack1.isEmpty()){
    
    
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

猜你喜欢

转载自blog.csdn.net/m0_49449205/article/details/112313957