zoukankan      html  css  js  c++  java
  • java栈

    个人理解:比较简单,用一个泛型数组和一个计数器指针来控制,实现出栈、入栈、返回栈顶元素。感觉有一些缺陷




    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3562239.html

    栈(stack),是一种线性存储结构,它有以下几个特点:
    (01) 栈中数据是按照"后进先出(LIFO, Last In First Out)"方式进出栈的。
    (02) 向栈中添加/删除数据时,只能从栈顶进行操作。

    栈通常包括的三种操作:push、peek、pop。
    push -- 向栈中添加元素。
    peek -- 返回栈顶元素。
    pop  -- 返回并删除栈顶元素的操作。

    /**
     * Java : 数组实现的栈,能存储任意类型的数据
     *
     * @author skywang
     * @date 2013/11/07
     */
    import java.lang.reflect.Array;
    
    public class GeneralArrayStack<T> {
    
        private static final int DEFAULT_SIZE = 12;
        private T[] mArray;
        private int count;
    
        public GeneralArrayStack(Class<T> type) {
            this(type, DEFAULT_SIZE);
        }
    
        public GeneralArrayStack(Class<T> type, int size) {
            // 不能直接使用mArray = new T[DEFAULT_SIZE];
            mArray = (T[]) Array.newInstance(type, size);
            count = 0;
        }
    
        // 将val添加到栈中
        public void push(T val) {
            mArray[count++] = val;
        }
    
        // 返回“栈顶元素值”
        public T peek() {
            return mArray[count-1];
        }
    
        // 返回“栈顶元素值”,并删除“栈顶元素”
        public T pop() {
            T ret = mArray[count-1];
            count--;
            return ret;
        }
    
        // 返回“栈”的大小
        public int size() {
            return count;
        }
    
        // 返回“栈”是否为空
        public boolean isEmpty() {
            return size()==0;
        }
    
        // 打印“栈”
        public void PrintArrayStack() {
            if (isEmpty()) {
                System.out.printf("stack is Empty
    ");
            }
    
            System.out.printf("stack size()=%d
    ", size());
    
            int i=size()-1;
            while (i>=0) {
                System.out.println(mArray[i]);
                i--;
            }
        }
    
        public static void main(String[] args) {
            String tmp;
            GeneralArrayStack<String> astack = new GeneralArrayStack<String>(String.class);
    
            // 将10, 20, 30 依次推入栈中
            astack.push("10");
            astack.push("20");
            astack.push("30");
    
            // 将“栈顶元素”赋值给tmp,并删除“栈顶元素”
            tmp = astack.pop();
            System.out.println("tmp="+tmp);
    
            // 只将“栈顶”赋值给tmp,不删除该元素.
            tmp = astack.peek();
            System.out.println("tmp="+tmp);
    
            astack.push("40");
            astack.PrintArrayStack();    // 打印栈
        }
    }

    2. Java实现二:Java的 Collection集合 中自带的"栈"(stack)的示例

    import java.util.Stack;
    
    /**
     * Java : java集合包中的Stack的演示程序
     *
     * @author skywang
     * @date 2013/11/07
     */
    public class StackTest {
    
        public static void main(String[] args) {
            int tmp=0;
            Stack<Integer> astack = new Stack<Integer>();
    
            // 将10, 20, 30 依次推入栈中
            astack.push(10);
            astack.push(20);
            astack.push(30);
    
            // 将“栈顶元素”赋值给tmp,并删除“栈顶元素”
            tmp = astack.pop();
            //System.out.printf("tmp=%d
    ", tmp);
    
            // 只将“栈顶”赋值给tmp,不删除该元素.
            tmp = (int)astack.peek();
            //System.out.printf("tmp=%d
    ", tmp);
    
            astack.push(40);
            while(!astack.empty()) {
                tmp = (int)astack.pop();
                System.out.printf("tmp=%d
    ", tmp);
            }
        }
    }
  • 相关阅读:
    Cocos2d-x 内存管理
    Cocos2d-x 解惑
    前端最实用、全面的工具类方法
    Java web 项目获取时间的方式列举
    Win系统常用指令
    Js三级下拉列表联动
    Js数组操作
    常用网站推荐
    最常用的正则表达式
    Oracle数据库学习
  • 原文地址:https://www.cnblogs.com/tabCtrlShift/p/9440558.html
Copyright © 2011-2022 走看看