introduce
"To become a master, will practice this work."
To be a good programmer, data structures and algorithms are compulsory content. And now the Web programmers use traditional algorithms and data structures are relatively few, because many algorithms are packaged well, do not need us to worry about the specific implementation details, such as PHP stack operation Array_pop, stack operation Array_push, there are specified library functions, leads to less research on basic algorithms and finally a tool puppet.
So I suggest that more coder start learning from the basics. This is the first to talk about our most familiar stack operation began, let us familiar with the stack.
What is a stack?
The formula "backward first out", this is my impression of the deepest words, but also a lump in the teacher's explanation, the most impressive.
Definition: A stack is a linear table that restricts insertions and deletions to only one position, which is the end of a linear table called the top of the stack.
Process: The first data is pushed into the bottom of the stack, the last data on the top of the stack, you need to read data from the top start pop-up data (the last data was first read out).
Analysis
By definition and process, we analyze the data structure (red identification), action part (blue identification), and action rules (yellow logo).
Please see
Component Composition
Data: Linear table (stored with array structure named data), the end index (with the int structure is named ending, the initial value is null--because the beginning linear table is not element, so there is no end index so said, and because of constantly fetching data, add data, this end is the changing element. )。
Action (method): Press in (push: rule, put on the last side of linear table), pop (pop: rule, remove from last, and end position move forward).
Coding
Run results
Summary
The above is my analysis and understanding of the stack process, because I am a php coder, so I use the angle of PHP to analyze and encode.
If the C language is encoded, the array should specify the maximum width, because the C-language array does not grow as a PHP array, and must have an initial width.
Original: http://www.cnblogs.com/baochuan/archive/2012/06/02/2530386.html