--- stack data structure and four Arithmetic

Suppose we asked to enter an expression like this: (3-1) * 10 + 3/9 + 2 , outputs the result. We know that before the parentheses, then multiplication and division, addition and subtraction Finally, when the high school using scientific calculator, is allowed to enter the results of such an expression, so the computer knows how to count the string inside the parentheses multiplication and division and then count it? We stack this data structure at first to introduce, again to solve this problem.

 

Even table has already been said array, now, the data structure of a linear additional tables --- stack.

For comparison vivid example, washing the dishes when it is not piled on top of one by one to put, take the time to also take a one from above, first put at the bottom, last place in the top, take when the first one to get. This is a typical stack structure. Advanced after First In Last Out (FILO).

 

How to implement a stack structure of it, the stack is also a linear table, there are data structures in front of an array of linked lists and reference to these two very basic linear table structure. The first stack is actually a special list or array. Can be implemented based on an array or linked list, an array become link stack or stacks, linked lists, and arrays having associated therewith characteristics.

Special point stack is advanced to the elements on the low stack backward in the top of the stack. Insert an element to the stack call stack, push, push all right, insert the data will be placed on the stack. Remove an element from the stack call out the stack, unstack all right, then remove the original stack of this element will be deleted, following that element becomes the new top of the stack.

Stack array element is generally low index of the stack starts, the growth index go out push down direction; a low stack link stack is typically the first node, the end node is the top of the stack.

Since they are using an array or linked list to achieve, why carry out a separate data structure yet. Arrays and linked lists exposes too many operations. It will be more prone to error. Targeted stack package out such a configuration, will be more suitable in certain scenarios. Imagine our browser's forward and back, is not it like two stacks of data exchange operation, a forward stack, stack a retreat. Point back, back to the top of the stack pop stack, stack forward into the top of the stack; then point forward, is not that pressed forward into the back of the stack top element of the stack. So alternately with each other.

Imagine a program call flow is not also a stack structure. The last method call executed first.

 

Java inside Stack also array-based implementation, it inherits the Vector . We implement a simple stack the basic operation of an array:

 

package com.nijunyang.algorithm.stack;

/**
 * Description:
 * Created by nijunyang on 2020/4/1 23:48
 */
public class MyStack<E> {

    private static final int DEFAULT_SIZE = 10;

    private Object[] elements;

    private int size;

    public MyStack() {
        this(DEFAULT_SIZE);
    }

    public MyStack(int capacity) {
        this.elements = new Object[capacity];
    }

    /**
     * 入栈
     * @param E
      * / 
    public  void Push (E E) {
         // elastically stretchable, expansion / contraction free up memory 
        IF (size> = elements.length) { 
            a resize (size * 2 ); 
        } the else  IF (size> 0 && size <elements.length / 2 ) { 
            a resize (elements.length / 2 ); 
        } 
        Elements [size ++] = E; 
    } 

    / ** 
     * pop 
     * / 
    public E POP () {
         IF (isEmpty ()) {
             return  null ;  
        }
        E E= (E) Elements [- size];    // size is 5, then 4 is the last element that is --size 
        Elements [size] = null ;         // now has a size 4, and this element of pop is 4 positions to an empty 
        return E; 
    } 

    public  Boolean isEmpty () {
         return size == 0 ; 
    } 

    public  int size () {
         return size; 
    } 

    / ** 
     * expansion / contraction 
     * / 
    Private  void a resize ( int newCapacity) { 
        Object [ ] TEMP =   new new Object [newCapacity];
        for  (int in = 0; in <size; In ++ ) { 
            TEMP [in] = elements [in]; 
        } 
        Elements = TEMP; 
    } 

    Public  Static  Void main (String [] args) { 
        MyStack <integer> = myStack new MyStack (5 ); 
        myStack.push ( 1 ); 
        myStack.push ( 2 ); 
        myStack.push ( 3 ); 
        myStack.push ( 4 ); 
        myStack.push ( 5 ); 
        myStack.push ( 6 );
        System.out.println (myStack.size); 
        The integer = myStack.pop (); 
        System.out.println (e); 
        by = myStack.pop (); 
        System.out.println (e); 
        by = myStack.pop (); 
        System.out.println (e); 
        by = myStack.pop (); 
        System.out.println (e); 
        by = myStack.pop (); 
        System.out.println (e); 
        by = myStack.pop (); 
        System.out.println (e); 
        by = myStack.pop (); 
        System.out.println (e); 
    }


}

 

 

Now with a look at how we use the stack to solve (3-1) * 3 + 10/2 9+ this calculation

First we have to how to deal with priority brackets and operational sign it

Here to talk about infix and postfix expression, like this expression 9+ (3-1) * 3 + 10/2 that infix expression, if we convert 931--3 * + 102 / + this is the postfix, postfix expression also called inverse Poland , on their own or can be Baidu google, postfix notation is operating behind the two operands, and infix expression is operating on two operands symbol intermediate.

Look postfix expression is how the operation is to meet the operating sign put in front of two numbers symbolic computation:

931--3 * + 102 / + expression of this operation is as follows:

931-- this time took 3 and 1 obtained by subtracting 2 => 923 * this time took 2 and 3 are multiplied 6 =>

9 6 + => 15 10 2 / =>15 5 + => 20

Generally is such a process, the process is not like the stack operation, a numeral on the stack, symbol encountered two figures put the numbers in front of the stack is calculated, then the result onto the stack until the end of the expression.

 

Now we just convert the infix expression into postfix notation can be calculated. Look at Baidu conversion process

 

 

 

Is simply used to store a stack of the symbol, and then from left to right traverse infix expressions numerals and characters, if the digital outputs, if the priority of the symbol is determined and symbol stack, brackets, or if it is a low priority in the top element, and then sequentially outputs the stack, the current symbol into the stack until the last end.

 

9+(3-1)*3+10/2

 

Initialize a stack Stack , followed by traversing our infix expression, the operating logic is as follows:

 

  1. 9 Output => 9
  2. + Stack empty directly into the stack: Stack : +
  3. ( Unpaired directly into the stack: Stack : + (
  4. 3 digital direct output: 93
  5. - front ( directly into the stack: Stack + (-
  6. A direct output: 931
  7. )   Will pop up in front of the symbols output until a match to the first ( far: . 9. 3. 1 - Stack: +
  8. * Higher priority than the + into the stack: Stack : + *
  9. 3 Output 931--3
  10. + Lower priority than the top of the stack * stack after the stack pop whether the ratio of the output is determined to continue + low priority ( the same level also pops up higher than the top of the stack is limited, so far or empty stack ) , where there will be continuous pop * + then the current + stack: . 9. 3. 1 -. 3 + Stack *: +
  11. 10 Output: 931--3 + 10 *
  12. / Higher priority than the top of the stack + Direct stack: Stack : + /
  13. 2 direct output: 931--3 + 10 * 2
  14. The last symbol of the output sequence: 931--3 * + 102 / +

 

 

 

Logic can be seen from the above, regardless of the final calculation, or infix expressions are used in turn postfix stack such a data structure.

 

Guess you like

Origin www.cnblogs.com/nijunyang/p/12623863.html