美文网首页
栈和队列的相互转化

栈和队列的相互转化

作者: Frank_8942 | 来源:发表于2019-01-24 13:16 被阅读12次
    //队列转栈
    import java.util.LinkedList;
    import java.util.Queue;
     
    public class Queue_To_Stack {
        private static Queue<Integer> data ;  //原本的数据队列
        private static Queue<Integer> help ;    // 辅助队列
        
        public static void setStack(){  //定义一个栈
            data = new LinkedList<>();
            help = new LinkedList<>();
        }
        public static void push(int num){   //压入栈
            data.add(num);    
        }
        public static Integer pop(){    //出栈
            if(data.isEmpty()){
                throw new IllegalArgumentException("栈为空!") ;
            }
            while(data.size() > 1){   //全部出队列,直到最后一个
                help.add(data.poll()) ;
            }
            int res = data.poll();  //得到最后一个
            swpe();  // help  和   data交换
            return res ;
        }
        
        public static Integer peek(){   //栈顶元素
            if(data.isEmpty()){
                throw new IllegalArgumentException("栈为空!") ;
            }
            while(data.size() > 1){
                help.add(data.poll()) ;
            }
            int res = data.poll();
            data.add(res) ;
            swpe();
            return res ;
        }
        
        public static void swpe(){
            Queue<Integer> tmp = help;
            help = data;
            data = tmp;
        }
        public static void main(String[] args) {
            setStack();
            push(2);
            push(44);
            push(11);
            push(266);
            push(277);
            push(200);
            System.out.println(pop());
            System.out.println(peek());
        }
     
    }
    
    
    
    //栈转队列
    import java.util.Stack;
     
    public class Stack_To_Queue {
        private static  Stack<Integer> data;
        private static Stack<Integer> help ;
        
        public static void setQueue(){
            data = new Stack<>();
            help = new Stack<>();
        }
        public static void add(int num){
            data.push(num) ;
        }
        public static Integer poll(){
            if(data.isEmpty() && help.isEmpty()){
                throw new RuntimeException("队列为空!") ;
            }else{
                while(data.size()>0){
                    help.push(data.pop()) ;
                }
                int res = help.pop();
                while(!help.isEmpty()){
                    data.push(help.pop()) ;
                }
                return res ;
            }
        }
        public static Integer peek(){
            if(data.isEmpty() && help.isEmpty()){
                throw new RuntimeException("队列为空!") ;
            }else{
                while(data.size()>0){
                    help.push(data.pop()) ;
                }
                int res = help.peek();
                while(!help.isEmpty()){
                    data.push(help.pop()) ;
                }
                return res ;
            }
        }
        public static void main(String[] args) {
            setQueue();
            add(1);
            add(2);
            add(3);
            add(4);
            add(5);
            System.out.println(poll());
            System.out.println(poll());
            System.out.println(poll());
            System.out.println(poll());
            System.out.println(poll());
        }
    }
    
    

    相关文章

      网友评论

          本文标题:栈和队列的相互转化

          本文链接:https://www.haomeiwen.com/subject/kzuyjqtx.html