• 数据结构-栈和队列


    栈是非常常见的数据结构,比如操作系统会给每个线程创建一个栈用来存储函数调用时哥哥函数的参数、返回地址和临时变量等。栈的特点是后进先出,最后被压入(push)的元素会被第一个弹出(pop)

    通常栈是一个不考虑排序的数据结构,需要O(n)才能找到最大或者最小元素

    队列是另一种很常见的数据结构,和栈不同,队列的特点是先进先出,即第一个进入队列的元素将会第一个出来。在广度优先算法中用队列

    栈和队列虽然是针锋相对的两个数据结构,但有意思的是他们会互相联系

    面试题9.用两个栈实现一个队列

    题目:用两个栈实现一个队列,声明如下,请事先两个函数addendTail和deleteHead,分别完成在队列尾部插入节点和队列头部删除节点的功能

    思路:

    两个栈,stack1负责模拟添加队尾元素,stack2负责模拟弹出队首元素

    每当元素需要加入队列,那么就会把stack2的元素返回stack1,最后往stack1中压栈

    每当元素需要弹出,就把stack1中的元素弹出,弹入到stack2中,最后弹出栈顶元素

    public class stackQueue {
        Stack<Integer> stack1=new Stack<>();
        Stack<Integer> stack2=new Stack<>();
    
        public void appendTail(int num){
            if(stack2.empty()){
                stack1.push(num);
                System.out.println(num+" push success!");
            }
            else{
                while(!stack2.empty()){
                    stack1.push(stack2.pop());
                }
                stack1.push(num);
                System.out.println(num+" push success!");
            }
        }
    
        public int deleteHead(){
            int tmp = -1;
            if(stack2.size()==0){
                while(!stack1.empty()){
                    tmp=stack1.pop();
                    stack2.push(tmp);
                }
            }
            else{
                tmp=stack2.peek();
            }
            if(tmp>0){
                System.out.println(tmp+" pop success!");
            }
            return stack2.pop();
        }
    
        public void queueSize(){
            System.out.println("queue total size: "+stack1.size()+stack2.size());
        }
    
        public static void main(String[] args){
            stackQueue sq = new stackQueue();
            sq.appendTail(12);
            sq.appendTail(13);
            sq.appendTail(14);
            sq.deleteHead();
            sq.deleteHead();
            sq.appendTail(15);
            sq.deleteHead();
            sq.queueSize();
        }
    }

     

    相关题目:用两个队列实现一个栈
    public class queueStack {
        private LinkedList<Integer> queue1=new LinkedList<>();
        private LinkedList<Integer> queue2=new LinkedList<>();
    
        public void push(int value) throws InterruptedException {
            if(queue2.isEmpty()){
                queue1.addLast(value);
            }
            else{
                while(!queue2.isEmpty()){
                    queue1.addLast(queue2.pollFirst());
                }
                queue1.addLast(value);
            }
            System.out.println(value+" push success!");
        }
    
        public void pop(){
            int tmp=-1;
            if(!queue1.isEmpty()){
                while(queue1.size()!=1){
                    queue2.addLast(queue1.pollFirst());
                }
                tmp=queue1.pollFirst();
            }
            if(tmp>0){
                System.out.println(tmp+" pop success!");
            }
            else if(tmp==-1){
                System.out.println("stack empty!");
            }
        }
    
        public void size(){
            System.out.println("stack size: "+queue1.size()+queue2.size());
        }
    
        public static void main(String[] args) throws InterruptedException {
            queueStack qs=new queueStack();
            qs.push(1);
            qs.pop();
            qs.push(2);
            qs.push(3);
            qs.push(4);
            qs.pop();
            qs.size();
        }
    
    }

  • 相关阅读:
    算法导论2.37答案
    算法导论2.37的算法
    heavy dark读《《暗时间》》
    深入SetOP2函数
    c++标准库都有哪些文件
    c++ sort函数的用法
    深入char转换为int/unsigned int的内部机制分析
    顺序容器之vector
    java的动态代理机制详解
    java.lang.IllegalStateException: Web app root system property already set to different value
  • 原文地址:https://www.cnblogs.com/ak918xp/p/14425801.html
Copyright © 2020-2023  润新知