The stack is one of the linear tables, and his characteristic is after-in first-out, it can be understood that the stack is like a box to store things, first put in the bottom, and then put in the upper layer, because the upper layer of things to the bottom of the thing, the lower level of want to go out must be the top of the first to take off.
Introduction Code:
Data class: This is the class that holds it. () is the thing to put on the stack
Stack class: Is the class of the stack, the entire stack is in this class
Main methods:
The stack push_stack ($data) detects if the stack is full, and if it is not full, let the data in the stack.
Stack pop_stack ($data) Check whether the stack is empty, if not empty can be out of the stack
Reads the stack top element top_stack () if the stack is not empty, returns the data at the top of the current stack.
Below is the code:
<?php/*** Author been**/class data{//Data private $data; Public function construct ($data) {$this->data= $data; echo $data. ": Brother into the Stack! <br> "; } public Function GetData () {return $this->data; The public Function destruct () {echo $this->data. : Brother's gone! <br> "; }}class stack{Private $size; Private $top; Private $stack =array (); Public function construct ($size) {$this->init_stack ($size); }//Initialize stack public function Init_stack ($size) {$this->size= $size; $this->top=-1; }//Determine if the stack is empty public function empty_stack () {if ($this->top==-1) return 1; else return 0; }//Determine if the stack is full public function full_stack () {if ($this->top< $this->size-1) return 0; else return 1; }//into the stack public function Push_stack ($data) {if ($this->full_stack ()) echo "Stack full <br/>"; else $this->stack[++ $this->top]=new data ($data); }//out-Stack public function pop_stack () {if ($this->empty_stack ()) echo "Stack empty <br/>"; else unset ($this->stack[$this->top--]); }//Read stack top element public function Top_stack () {return $this->empty_stack ()? " Stack empty no data! ": $this->stack[$this->top]->getdata (); }} $stack =new stack (4), $stack->pop_stack (), $stack->push_stack ("AA"), $stack->push_stack ("Aa1"); $stack- >pop_stack ("Aa1"), $stack->push_stack ("Aa2"), $stack->push_stack ("Aa3"), $stack->push_stack ("aa4"); echo $stack->top_stack (), ' <br/> '; $stack->push_stack ("Aa5"); $stack->push_stack ("Aa6"); $stack Pop_stack (); $stack->pop_stack (); $stack->pop_stack (); $stack->pop_stack (); $stack->pop_stack (); $ Stack->pop_stack ();
Operation Result:
Empty stack of AA: Brother into the Stack! Aa1: Brother into the Stack! Aa1: Brother's gone! Aa2: Brother into the Stack! Aa3: Brother into the Stack! AA4: Brother into the Stack! AA4 stack full of aa4: Brother gone! Aa3: Brother's gone! Aa2: Brother's gone! AA: Brother has gone! Empty stack, empty stack.
Case: Stack-based Advanced calculator
When we get a string expression, how do we get to the result of its operation?
At this point we can use the algorithm of the stack to solve this problem skillfully.
The idea is this: (We use PHP function substr loop to intercept this string formula, and then take the value of the string "We have to start from the first character intercept", we will start to intercept the position as a loop-growing variable, initialized to "$index = 0"), Also need to create two stacks, a dedicated to hold the number "$numStack", a storage Operator "$operStack", we also need a function that can determine whether an operation symbol, the value of each intercept into the custom function, return a can be distinguished from the number or operator of the identity, By determining whether the value is a number or an operator, the number is inserted into the stack and the operator is the symbol stack. Insert a stack of words can be inserted directly, but the symbol stack, you need to special deal with ["If the symbol stack is empty, insert directly, not empty: we want to compare the inserted symbol with the symbol inside the stack, we can define a function to determine the priority of the symbol, the * and/assume 1 + and-assumed to be 0 Assuming that the number has a high priority, the operator priority can be obtained, and when the symbol priority to be inserted is less than the precedence of the operator at the top of the stack, a two-value symbol stack pops up from the stack to perform the operation of the operator "
Here is an example of PHP "reference from Hanshunping Teacher's PHP algorithm tutorial"
Operation Result:
12+5*2+3-5*2=15