zoukankan      html  css  js  c++  java
  • Java中栈结构的自我实现

    package com.pinjia.shop.common.collection;
    
    /**
     * Created by wangwei on 2017/1/3.
     */
    public class MyLinkedStack<T> {
        //定义节点数据结构
        private class Node{
            public T data;
            public Node next;
            public Node(){}
            public Node(T data,Node next){
                this.data = data;
                this.next = next;
            }
        }
    
        //栈顶元素
        private Node top;
        //元素个数
        private int size;
        //插入元素
        public void push(T element){
            top = new Node(element,top);
            size++;
        }
        //出栈操作
        public T pop(){
            Node oldNode = top;
            top = top.next;
            //释放引用
            oldNode.next = null;
            size--;
            return oldNode.data;
        }
    
        //返回栈顶元素,但不出栈
        public T peek(){
            return top.data;
        }
    
        //栈长度
        public int length(){
            return size;
        }
    
        //判断链栈是否为空栈
        public boolean empty(){
            return size == 0;
        }
    
        public String toString(){
            //链栈为空时
            if(empty())
                return "[]";
            else{
                StringBuilder sb = new StringBuilder("[");
                for(Node current = top;current != null;current = current.next){
                    sb.append(current.data.toString()+", ");
                }
                int len = sb.length();
                return sb.delete(len-2,len).append("]").toString();
            }
        }
        public static void main(String[] args) {
            MyLinkedStack<String> stack = new MyLinkedStack<String>();
            // 不断地入栈
            stack.push("aaaa");
            stack.push("bbbb");
            stack.push("cccc");
            stack.push("dddd");
            System.out.println(stack);
            // 访问栈顶元素
            System.out.println("访问栈顶元素:" + stack.peek());
            // 弹出一个元素
            System.out.println("第一次弹出栈顶元素:" + stack.pop());
            // 再次弹出一个元素
            System.out.println("第二次弹出栈顶元素:" + stack.pop());
            System.out.println("两次pop之后的栈:" + stack);
        }
    }
    

      

    以下是基于数组的实现:

    package com.pinjia.shop.common.collection;
    
    /**
     * Created by wangwei on 2017/1/3.
     */
    public class MyArrayStack<T> {
        private Object[] objs = new Object[16];
        private int size = 0;
    
        public boolean isEmpty() {
            return size == 0;
        }
    
        public void clear() {
            // 将数组中的数据置为null, 方便GC进行回收
            for (int i = 0; i < size; i++) {
                objs[size] = null;
            }
            size = 0;
        }
    
        public int length() {
            return size;
        }
    
        public boolean push(T data) {
            // 判断是否需要进行数组扩容
            if (size >= objs.length) {
                resize();
            }
            objs[size++] = data;
            return true;
        }
    
        /**
         * 数组扩容
         */
        private void resize() {
            Object[] temp = new Object[objs.length * 3 / 2 + 1];
            for (int i = 0; i < size; i++) {
                temp[i] = objs[i];
                objs[i] = null;
            }
            objs = temp;
        }
    
        public T pop() {
            if (size == 0) {
                return null;
            }
            return (T) objs[--size];
        }
    
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("MyArrayStack: [");
            for (int i = 0; i < size; i++) {
                sb.append(objs[i].toString());
                if (i != size - 1) {
                    sb.append(", ");
                }
            }
            sb.append("]");
            return sb.toString();
        }
    
        public static void main(String[] args) {
            MyArrayStack<Integer> stack = new MyArrayStack<Integer>();
            stack.push(12);
            stack.push(13);
            System.out.println(stack.length());
            System.out.println(stack);
        }
    }
    

      

  • 相关阅读:
    (转)单机上配置hadoop
    整数划分 Integer Partition(二)
    整数划分 Integer Partition(一)
    深入理解计算机系统:信息的处理和表示(二)整数四则运算
    深入理解计算机系统:信息的处理与表示(一)基础
    从《营造法式》为何成书于北宋 谈起
    (转)排列算法 Permutation Generation
    洛谷2971 [USACO10HOL]牛的政治Cow Politics
    洛谷1549 棋盘问题(2)
    洛谷3084 [USACO13OPEN]照片Photo
  • 原文地址:https://www.cnblogs.com/shenlanzhizun/p/6245004.html
Copyright © 2011-2022 走看看