Analog data structures stack array

I.e. the characteristics of the stack last out, using an array of analog stack, the stack to achieve this characteristic is defined mainly by a pointer (index).

The initial position of a pointer is -1

The following shows the code:

Package com.ebiz.stack; 

/ ** 
 * @author YHJ 
 * @Create 2019-07-20 14:20 
 * analog stack array 
 * / 

public  class ArrayStack { 

    Private   int the maxSize;
     Private   int [] ARR;   // array stack simulation 
    Private   int Top = -1 ; 

    // constructor initializes the array 
    public ArrayStack ( int the maxSize) {
         the this .maxSize = the maxSize;
         the this .arr = new new  int [the maxSize]; 
    } 

    // verify stack full 
    public Boolean isFull () {
         return Top-== the maxSize. 1 ; 
    } 

    // verify empty 
    public  Boolean isEmpty () {
         return Top == -1 ; 
    } 

    // stack 
    public  void Push ( int value) {
         IF (isFull () ) { 
            System.out.println ( "full stack" );
             return ; 
        } 
        Top ++ ; 
        ARR [Top] = value; 
    } 

    // pop 
    public  intPOP () {
         IF (isEmpty ()) {
             the throw  new new a RuntimeException ( "empty stack" ); 
        } 
        int value = ARR [Top]; 
        Top - ;
         return value; 
    } 

    // traversable stack 
    public  void List () {
         IF (isEmpty ()) {
             the throw  new new a RuntimeException ( "empty stack" ); 
        } 
        the while ( to true ) {
             IF (isEmpty ()) { 
                System.out.println ( "empty stack" );
                BREAK ; 
            } 
            System.out.printf ( "Outbound element is n-% D%" , ARR [Top]); 
            Top - ; 
        } 
    } 
}

Analog stack array, given pop, push, List few simple methods, test classes are given below,

Package com.ebiz.stack; 

/ ** 
 * @author YHJ 
 * @Create 2019-07-20 15:11 
 * / 
public  class the Test { 

    public  static  void main (String [] args) { 

        // initialize stack 
        ArrayStack Stack = new new ArrayStack (. 5 ); 

        // add elements 
        for ( int I =. 1; I <=. 5; I ++ ) { 
            stack.push (I); 
        } 

        // Get the stack 
        System.out.println (stack.pop ()); 
        System.out.println (stack.pop ()); 

        // traverse the stack 
        stack.list ();

    }
}

 

Guess you like

Origin www.cnblogs.com/jiushixihuandaqingtian/p/11240980.html