首页 > 其他 > 详细

悔恨当初抛数构,如今学起虐成狗(队列与栈之间的转换)

时间:2016-03-29 14:16:38      阅读:191      评论:0      收藏:0      [点我收藏+]

  学过数据结构的都应该对栈和队列有一定的了解。。。反正我是没学好。。。现在很后悔。。。

记得应该是大二上学期的时候吧。。。我们体验到数据结构这门课。。。现在想想当初天天一上课就睡觉。。。

哎。。。想多了都是泪。。。就没有好好上过。。。大二基本荒废在网络游戏的世界里了。。。

如果想想当初能够好好学习这门课。。也许现在的自己可能就是理想中的自己咯。。。

  对于一名攻城狮来说。。。如果你不是想敲一天和尚撞一天钟的话。。。那么就

请好好对待数据结果这门课。。。因为他太重要了。。。太重要了。。。太重要了。。。

以至于现在我只能拼了命的在补缺补漏。。。是的。。。书到用时方恨少。。。

来吧。。伙伴们。。。我们就开始我们今天的补缺补漏点吧。。。

Let‘s GO!!!

 

  队列:火车过隧道。。。火车头先进隧道先出隧道、、、先进先出

  栈:桶里倒水再倒出。。。先进后出。。。

两个堆栈实现队列与两个队列实现堆栈

package Test2016.test;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Test20160329 {

    public static void main(String[] args) {
    
        /**
         * @desc 两个堆栈实现队列
         * @idea 堆栈:先进后出,队列:先进先出
         * */
        MyQueue myQueue = new MyQueue();
        
        System.out.println("入队列顺序:");
        for (int i = 1; i <= 10; i++) {
            System.out.print(i + " ");
            myQueue.push_q(i);
        }
        
        System.out.println("\n用堆栈实现【队列】弹出:");
        for (int i = 0; i < 10; i++) {
            System.out.print(myQueue.pop_q() + " ");
        }
        
        System.out.println("\n######################");
        
        /**
         * @desc 两个队列实现堆栈
         * @idea 堆栈:先进后出,队列:先进先出
         * */
        MyStack myStack = new MyStack();
        
        System.out.println("入堆栈顺序:");
        for (int i = 1; i <= 10; i++) {
            System.out.print(i + " ");
            myStack.push_s(i);
        }
        
        System.out.println("\n用队列实现【堆栈】弹出:");
        for (int i = 0; i < 10; i++) {
            System.out.print(myStack.pop_s() + " ");
        }
        
    }
}

class MyQueue {
    
    private Stack<Integer> s1 = null;
    private Stack<Integer> s2 = null;
    
    public MyQueue() {
        s1 = new Stack<Integer>();
        s2 = new Stack<Integer>();
    }
    
    public Integer push_q(Integer p) {
        s1.push(p);
        return p;
    }
    
    public Integer pop_q() {
        Integer res = null;
        
        if (!s2.isEmpty()) {
            res = s2.pop();
        } else {
            while (!s1.isEmpty()) {
                res = s1.pop();
                s2.push(res);
            }
            
            if (!s2.isEmpty()) {
                res = s2.pop();
            }
        }
        return res;
    }
}


class MyStack {
    
    private Queue<Integer> q1 = null;
    private Queue<Integer> q2 = null;
    
    public MyStack() {
        q1 = new LinkedList<Integer>();
        q2 = new LinkedList<Integer>();
    }
    
    public Integer push_s(Integer p) {
        q1.add(p);
        return p;
    }
    
    public Integer pop_s() {
        Integer res = null;
        boolean flag = true;    //标识变量

        if (q2.size() > 0 && flag == true) {
            flag = false;
            while (q2.size() > 0) {
                if (q2.size() > 0) {
                    res = q2.remove();
                    q1.add(res);
                }
            }
            q1.remove(res);
        } else if (q1.size() > 0 && flag == true) {
            while (q1.size() > 0) {
                if (q1.size() > 0) {
                    res = q1.remove();
                    q2.add(res);
                }
            }
            q2.remove(res);
        }
        
        return res;
    }
}

程序运行输出:

入队列顺序:
1 2 3 4 5 6 7 8 9 10 
用堆栈实现【队列】弹出:
1 2 3 4 5 6 7 8 9 10 
######################
入堆栈顺序:
1 2 3 4 5 6 7 8 9 10 
用队列实现【堆栈】弹出:
10 9 8 7 6 5 4 3 2 1 

 一大推的啰嗦话不如看代码来的实在。。。现在不会的学了就会。。。

 不会的你们赶紧Code起来吧。。。下一秒你就可以呵呵的来句。。。

这尼玛也忒简单了吧。。。

代码也许、可能、大概会存在点点滴滴的、一丁点的缺陷吧。。。。

怎么办、、、我也不缉盗。。。

最后偷偷的告诉你们。。。

很多公司的技术笔试题都可能会被要求写队列与栈之间的转换代码实现欧欧。。。

悔恨当初抛数构,如今学起虐成狗(队列与栈之间的转换)

原文:http://www.cnblogs.com/JimLy-BUG/p/5274587.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!