4, link stack implementation (java code)

1, CCN

public class Node<T> {
    public T data;
    public Node next;
}

2, implementation code

public  class Stack <T> {
     Private  static the Node bottom; // bottom of the stack pointer 
    Private  static the Node Top; // stack pointer 
    Private  static Integer size; // stack size of the current 

    / ** 
     * Initialization 
     * / 
    public  void initStack () { 
        bottom = Top = new new the Node (); 
        top.next = bottom; 
        size = 0 ; 
    } 

    / ** 
     * is empty 
     * 
     * @return 
     * / 
    public static  Boolean isEmpty () {
         IF (top.next == bottom) {
             return  to true ; 
        } 
        return  to false ; 
    } 

    / ** 
     * Drawing 
     * 
     * @param Element
      * / 
    public  void pushStack (T Element) { 
        the Node TEMP = new new the Node (); 
        temp.data = Element;
         IF (top.next == bottom) // first incoming operation 
        { 
            temp.next =  bottom;
            top.next = TEMP; 
        } the else { 
            temp.next = top.next; 
            top.next = TEMP; 
        } 
        size ++ ; 
    } 

    / ** 
     * pop 
     * / 
    public  void popStack () { 


        IF (isEmpty ()) { 
            System.out.println ( "no element stack!" ); 
        } the else { 
            System.out.println ( "pop operations:" + top.next.data + "" ); 
            top.next = top.next.next ; 
        } 
        size - - ;
    } 

    / ** 
     number of elements * 
     * 
     * @return : numerical values
      * / 
    public  int sizeStack () {
         return size; 
    } 

    / ** 
     * See top value 
     * / 
    public  static  void getTop () { 
        System.out.println ( " top value: "+ top.next.data); 
    } 

    / ** 
     * 
     * print elements into 
     * / 
    public  static  void printStack () { 
        the Node TEMP = top;
         IF  (isEmpty ()) {
            System.out.println ( " no element in the stack! ");
        } else {
            for (int i = 0; i < size; i++) {
                System.out.print(temp.next.data + " ");
                temp = temp.next;
            }
        }
        System.out.println();

    }

    public static void main(String[] args) {
        Stack<Integer> integerStack = new Stack<>();
        integerStack.initStack();
        integerStack.pushStack(1);
        printStack();
        integerStack.pushStack(2);
        printStack();
        integerStack.pushStack(3);
        printStack();
        integerStack.pushStack(4);
        printStack();

        integerStack.popStack();
        printStack();
        integerStack.pushStack(4);
        printStack();
        integerStack.popStack();
        printStack();

        System.out.println("大小:" + integerStack.sizeStack());

        getTop();
    }

3, the results show

. 1 
2. 1 
3 2. 1 
4321  
pop operations: . 4 
3 2. 1 
4321  
pop operations: . 4 
3 2. 1  
Size: 3 
Top Value: 3

 

Guess you like

Origin www.cnblogs.com/karrya/p/11031335.html