zoukankan      html  css  js  c++  java
  •   是限定仅在表尾进行插入或删除操作的线性表
      表尾称为栈顶,表头称为栈底
      特点:后进先出

      操作:
    1.推入push
    2.弹出pop

    栈的数组实现:

    public class ArrayStack<E> {
        
        private List<E> list = new ArrayList<E>();
        
        public boolean isEmpty(){
            return list.size()==0;
        }
        
        public void push(E element){
            list.add(element);
        }
        
        public void pop(){
            list.remove(list.size()-1);
        }
        
        public E getPop(){
            return list.get(list.size()-1);
        }    
        
        public List<Integer> getElements(){
            List<Integer> result = new ArrayList<Integer>();
            for(E e : list){
                result.add(((Element)e).getValue());
            }
            return result;
        }
    
    }

    栈的链表实现:

    public class LinkedStack<E>{
    
        private static class Node<E>{
    
            E element;
            
            Node<E> next;    
            
            public Node(E element){
                this.element = element;
            }
    
        }
        
        private Node<E> top = new Node<E>(null);
        
        private int size = 0;
        
        public boolean isEmpty() {
            return size == 0;
        }
    
        public void push(E element){
            Node<E> newNode = new Node<E>(element);
            if(!isEmpty()){
                newNode.next = getPopNode();
                top.next = newNode;
            }else{
                top.next = newNode;
            }
            size++;
        }
        
        public void pop(){
            if(isEmpty()){
                throw new RuntimeException("The stack is empty");
            }
            Node<E> firstNode = top.next;
            top.next = firstNode.next;
            firstNode.next = null;
            size--;
        }
        
        public E getPop(){
            return getPopNode().element;
        }    
        
        private Node<E> getPopNode(){
            if(isEmpty()){
                throw new RuntimeException("The stack is empty");
            }
            return top.next;
        }
        
        public List<Integer> getElements(){
            if(isEmpty()){
                return null;
            }else{
                List<Integer> elements = new ArrayList<Integer>();
                Node<E> node = (Node<E>) top;
                while(node.next!=null){
                    node = node.next;
                    elements.add(((Element)node.element).getValue());
                }
                return elements;
            }
        }
        
    }
  • 相关阅读:
    C#接口入门学习
    消息队列接收时报错:对消息队列系统的访问被拒绝
    给某做测试的好友的建议
    在不同的Sql Server 数据库服务器(不同机器)导数据。
    如何让开发人员更好测试?
    存储过程初探
    语音报警.NET开发初探
    vs2010下Siverlight开发环境安装
    C# HttpWebRequest 从google服务器获取google的PageRank PR值
    创建进程API CreateProcess Demo
  • 原文地址:https://www.cnblogs.com/tangyanbo/p/4282362.html
Copyright © 2011-2022 走看看