Table 2. Linear - Stack

1. What is the stack

    . [1] can only be inserted a stack and deletion linear form at one end; Insert: push (Push); Delete: popping (POP);

    [2] stack is in accordance with "last out" (Last In First Out, LIFO) principle of storing data;

           Top of the stack (Top): You can remove and insert;

           Stack bottom (Botton);

    . [3] stack Category:

              Static stack: its core is an array, similar to a continuous memory array, it is necessary to determine a good stack size in advance;

              Dynamic stack: its core is the list, under the premise of memory enough to be infinite, not continuous;

    [4] FIG stack:

 

1.1 stack representation

 

2. The basic operation of the stack

    [1]. Stack memory structure

    [2] The push

    [3]. Pop

    [4] The determination empty, full determination

    [5]. Traverse the stack

    [6]. Emptied stack

    Note: Static stack reference [2 linear meter - array]

         Reference [2 linear dynamic stack table - list]

 

3. Static stack code implementation

    [1] Static stack storage structure

public  class MyStack {
     Final  int the STACKSIZE =. 5; // declaration stack capacity
     // NOTE: final usage; 
    public  int bottom;
     public  int Top;
     public  int [] ARR; 

    public MyStack () { 
        bottom = Top = -1 ; 
        ARR = new new  int [the STACKSIZE]; 
    } 
}

 

 

 

    [2] Basic operation of the static stack implementation

 

Package com.Stack; 

/ * 
* static basic stack operations: 
* 1. initialization, creation, 
* the stack 2. 
* 3. Stack 
* 4. Analyzing empty; Analyzing full; 
* 5. traversal 
* 6. Empty 
* 
* * / 

public  class DemoStack {
     // Analyzing full 
    public  static  Boolean isFull (MyStack stack) {
         IF (stack.top> =-stack.STACKSIZE. 1) // use a static array of stacks constructed subscript is from 0 to 
            return  to true ;
         the else 
            return  to false ;
         // Analyzing full, abbreviated codes: return stack.top> =-stack.STACKSIZE. 1; 
    } 

    //Analyzing Empty 
    public  static  Boolean isEmpty (MyStack Stack) {
         IF (stack.top == stack.bottom)
             return  to true ;
         the else 
            return  to false ;
         // shorthand: == stack.bottom stack.top return; 
    } 

    // push 
    public  static  Boolean poshStack (MyStack stack, int Data) {
         IF (isFull (stack)) { 
            System.out.println ( "full stack this!" );
             return  to false ; 
        } the else { 
            stack.arr [stack.top ++] = Data;
             return  to true ; 
        } 
    } 

    // Stack 
    public  static  Boolean popStack (MyStack Stack) {
         IF (isEmpty (Stack)) { 
            System.out.println ( "current stack is empty!" );
             return  to false ; 
        } the else { 
            System.out.println ( "pop the top element:" + stack.arr [stack.top-- ]);
             return  to true ; 
        } 
    } 

    // iterate output 
    public  static  BooleanprintOutStack (MyStack Stack) {
         IF (isEmpty (Stack)) { 
            System.out.println ( "! current can not be output stack is empty" );
             return  to false ; 
        } the else {
             int length = stack.top - stack.bottom; 
            the System. Out.print ( "stack traverse output:" );
             for ( int I =-length. 1; I> = 0; i-- ) { 
                of System.out.print (stack.arr [I] + "" ); 
            } 
            the System .out.println ( "\ n-" + "delimiter ============== ===================" );
             return true;
        }
    }

    // 清空栈
    public static boolean clearStack(MyStack stack){
        if(isEmpty(stack)){
            System.out.println("当前栈为空!");
            return false;
        }else{
/*           int length = stack.top - stack.bottom;
             for(int i=length-1 ; i>=0; i--){
                 stack.arr[i] = 0;
                 stack.top--;
            }*/
            stack.arr = null;//Internal java will occasionally start] [garbage collection, the heap memory space recovered 
            stack.top = stack.bottom = -1 ;
             return  to true ; 
        } 
    } 


    public  static  void main (String [] args) { 
        MyStack Stack = new new MyStack (); 

        poshStack (Stack, . 1 ); 
        poshStack (Stack, 2 ); 
        poshStack (Stack, . 3 ); 
        poshStack (Stack, . 4 ); 
        poshStack (Stack, . 5 ); 
        System.out.println ( "the stack" ); 
        printOutStack (Stack);

        popStack(stack);
        popStack(stack);
        System.out.println("出栈后");
        printOutStack(stack);

        clearStack(stack);
        if(isEmpty(stack)){
            System.out.println("栈已清空 !");
        }
        printOutStack(stack);
    }
}

 

 

 

    [3]. Run Results

 

 

 

4. Dynamic code for stack (to be more)

    [1] Dynamic storage structure stack

    [2] Basic operation of dynamic stack

    [3]. Run Results

 

NOTE: The learning process will inevitably be a lot of error, please advise! Problems can comment directly to me, thank you!

Guess you like

Origin www.cnblogs.com/ZHOUsya/p/12642058.html