Based on the list of the stack (Java)

Package com.rao.linkList; 

/ ** 
 * @author Srao 
 * @className LinkedStack 
 * @date 2019/12/3 13:59 
 * @package com.rao.linkList 
 * @Description stack-based list 
 * / 
public  class LinkedStack { 

    / ** 
     * define node 
     * / 
    static  class the node {
         Private String Data;
         Private the node Next; 

        public the node (Data String) {
             the this .data = Data;
             the this .next = null ; 
        } 

        public String getData() {
            return data;
        }
    }

    //栈顶元素
    private Node top;

    /**
     * 入栈
     * @param s
     */
    public void push(String s){
        Node node = new Node(s);
        if (top == null){
            top = node;
        }else {
            node.next = top;
            top = node;
        }
    }

    /**
     * 出栈
     * @return
     */
    public Node pop(){
        Node node = null;
        if (top.next != null){
            node = top;
            top = top.next;
        }else {
            node = top;
            top = null;
        }
        return node;
    }

    public static void main(String[] args) {
        LinkedStack linkedStack = new LinkedStack();
        linkedStack.push("aa");
        linkedStack.push("11");
        linkedStack.push("@@");
        System.out.println(linkedStack.top.getData());
        System.out.println(linkedStack.pop().getData());
        System.out.println(linkedStack.pop().getData());
        System.out.println(linkedStack.pop().getData());
    }
}

Based on the list based on the new stack and are different stack array, linked list based stack must define your own node, and a stack of arrays based array as a node, the node may be defined for use to achieve an internal class, a new instance of each class a new node

Guess you like

Origin www.cnblogs.com/rao11/p/11976452.html