The definition of PHP stack, the method of inbound and outbound stack, and the complete example of the calculator Based on stack implementation, php Stack

Source: Internet
Author: User

The definition of PHP stack, the method of inbound and outbound stack, and the complete example of the calculator Based on stack implementation, php Stack

This article describes the definition of the PHP stack, the method of the inbound stack and the calculator Based on the stack implementation. We will share this with you for your reference. The details are as follows:

Stack is a linear table. It features a post-import, first-out, which can be understood as a box for storing things, and then put it in the upper layer. Because the upper layer holds down the bottom layer, the lower layer must be removed first.

Introduction code:

Data class: the class that stores data. () Is the thing to put into the stack.
Stack class: it is a stack class. The entire stack is in this class.

Main Methods:

Push_stack ($ data) checks whether the stack is full. If it is not full, the data is imported into the stack.
Out-of-stack pop_stack ($ data) checks whether the stack is empty.
Read the top_stack () element at the top of the stack. If the stack is not empty, the data at the top of the current stack is returned.

Below is the code:

<? Php/*** Author Been **/class data {// data private $ data; public function _ construct ($ data) {$ this-> data = $ data; echo $ data. ": Brother entered the stack! <Br> ";} public function getData () {return $ this-> data;} public function _ destruct () {echo $ this-> data. ": brother is gone! <Br> ";}} class stack {private $ size; private $ top; private $ stack = array (); public function _ construct ($ size) {$ this-> Init_Stack ($ size) ;}// initialize the public function Init_Stack ($ size) {$ this-> size = $ size; $ this-> top =-1 ;}// judge whether the stack is empty public function Empty_Stack () {if ($ this-> top =-1) return 1; else return 0;} // determine whether the stack is full public function Full_Stack () {if ($ this-> top <$ this-> size-1) return 0; else return 1;} // inbound Stack public function Push_Stack ($ data) {if ($ this-> Full_Stack () echo "the stack is full <br/> "; else $ this-> stack [++ $ this-> top] = new data ($ data);} // public function Pop_Stack () {if ($ this-> Empty_Stack () echo "the stack is empty <br/> "; else unset ($ this-> stack [$ this-> top --]);} // read the stack top element public function Top_Stack () {return $ this-> Empty_Stack ()? "The stack is empty and there is 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 ();

Running result:

Stack empty aa: brother is in the stack! Aa1: brother is on the stack! Aa1: brother is gone! Aa2: brother is on the stack! Aa3: brother is on the stack! Aa4: brother is on the stack! The aa4 stack is full. The stack is full. aa4: Go! Aa3: brother is gone! Aa2: brother is gone! Aa: brother is gone! Stack is empty. Stack is empty.

Case: stack-based advanced Calculator

How can we obtain the computation result of a string operator?

At this time, we can use the stack algorithm to skillfully solve this problem.

The idea is as follows: (we use the php function substr loop to intercept this string operator and extract the value of this string in sequence [we have to intercept it from the first character ], we will start to take the screenshot and set it to a variable with a circular growth and initialize it to [$ index = 0]). At the same time, we also need to create two stacks, one dedicated to storing numbers [$ numStack ], for a storage operator [$ operStack], we also need a function that can judge whether it is an operator number and place the value intercepted each time in this custom function, return an identifier that can be differentiated from a number or operator. Determine whether the value is a number or operator. If the value is a number, insert the number stack. If the value is an operator, insert the symbol stack. Insert a number stack can be directly inserted, but the symbol stack needs special processing [[if the symbol stack is empty, insert it directly, not empty: we need to compare the inserted symbol with the symbol in the stack for calculation priority (a function can be defined to determine the symbol priority, suppose * And/as 1 + and-as 0, and assume that the priority of the number is high, so that the operator priority can be obtained ), when the priority of the symbol to be inserted is smaller than or equal to the operator priority at the top of the stack, two value symbol stacks are displayed. One operator is displayed to calculate them]

The following is an example of php. [Refer to the php algorithm tutorial from Han shunping]

<Html> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.