• 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());
            }
            
            
        }
    
    }
  • 相关阅读:
    expect脚本实例
    Linux dialog详解(图形化shell)
    makefile——小试牛刀
    gdb入门
    linux常见系统调用函数列表
    linux前后台任务的切换以及执行暂停
    centos 7.0 lnmp安装部署步骤
    环境列表
    setjmp与longjmp非局部跳转函数的使用
    malloc,calloc,alloca和free函数
  • 原文地址:https://www.cnblogs.com/phyxis/p/5194748.html
Copyright © 2020-2023  润新知