zoukankan      html  css  js  c++  java
  • list 实现队列 、栈

    package com.ijiami.main.shiro.test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 
     * @author Administrator
     * list 实现栈、队列
     *
     */
    public class QueueAndStack {
        int size = 0;
        List list = new ArrayList();
        
        public void putList(Object o){
            this.list.add(o);
            size = list.size();
        }
        public Object getStack(){
            List stack = new ArrayList();
            Object o = list.get(list.size()-1);
            for (int i = 0; i < list.size()-1; i++) {
                stack.add(list.get(i));
            }
            list = stack;
            size = list.size();
            return o;
            
        }
        
        public Object getQueue(){
            List queue = new ArrayList();
            Object o = list.get(0);
            for (int i = 1; i <= list.size()-1; i++) {
                queue.add(list.get(i));
            }
            list = queue;
            size = list.size();
            return o; 
        }
        
        public static void main(String[] args) {
            
            QueueAndStack heapAndStack = new QueueAndStack();
            heapAndStack.putList("1");
            heapAndStack.putList("2");
            heapAndStack.putList("3");
            int length = heapAndStack.size;
            for (int i = 0; i < length; i++) {
                System.out.println(heapAndStack.getQueue());
            }
            System.out.println("----");
            heapAndStack.putList("4");
            heapAndStack.putList("5");
            length = heapAndStack.size;
            for (int i = 0; i < length; i++) {
                System.out.println(heapAndStack.getQueue());
            }
            
            System.out.println("----");
            QueueAndStack heapAndStack0 = new QueueAndStack();
            heapAndStack0.putList("1");
            heapAndStack0.putList("2");
            heapAndStack0.putList("3");
            length = heapAndStack0.size;
            for (int i = 0; i < length; i++) {
                System.out.println(heapAndStack0.getStack());
            }
            System.out.println("----");
            heapAndStack0.putList("4");
            heapAndStack0.putList("5");
            length = heapAndStack0.size;
            for (int i = 0; i < length; i++) {
                System.out.println(heapAndStack0.getStack());
            }
            
            
        }
    
    }
  • 相关阅读:
    Linux下安装MySql8
    root与alias主要区别
    TCP/UDP 常用端口列表
    kafka文档
    kafka监测工具(可视化)
    Kafka消息队列
    postgresql
    gulp babel 配置不报错也没有输出结果的原因
    gulp中常用的模块
    判断浏览器标签页是隐藏或者显示状态
  • 原文地址:https://www.cnblogs.com/phyxis/p/5194748.html
Copyright © 2011-2022 走看看