On the stack of PHP data structure

Source: Internet
Author: User

Today began to step up their own PHP, first of all the programming language need to cultivate their own "internal strength", what is the "internal strength of the program ape", I think is the data structure and algorithm it. After all, is the soul, is the ordinary program ape to the advanced procedure Ape's advance.

Not much to say. Directly say the topic-"stack".

What is the stack, the so-called stack is to follow the "LIFO" principle.

The last stack of the advanced stack.

Using PHP to implement the stack does not need to consider the case of stack overflow, relatively easy to implement, for example, the following is the code after learning and references.


<?php
Class stack{
Private $data =array ();//Definition stack
Private $end =null;//defines the stack pointer. Also become the top of the stack.
To define a stack-in operation
Public function push ($data) {
if ($this->end===null)
$this->end=0;
Else
$this->end++;
$this->data[$this->end]= $data;
PHP is a weak class language, regardless of overflow
}
Out of the stack
Public Function pop () {
if (Empty ($this->data))
return false;
$ret = $this->data[$this->end];
Array_splice ($this->data, $this->end);//The array moves forward one
$this->end--;
return $ret;
}
Fetch stack
Public Function Get_stack () {
return $this->data;
}
}
?>
The above is the basic operation of the stack, and then by using the written stack to implement an online decimal conversion n tool. (n is less than or equal to 10. If you need to make a decimal or higher conversion needs to add functionality)
<?php
Class stack{
Private $data =array ();//Definition stack
Private $end =null;//defines the stack pointer and also becomes the top of the stack.


To define a stack-in operation
Public function push ($data) {
if ($this->end===null)
$this->end=0;
Else
$this->end++;
$this->data[$this->end]= $data;
PHP is a weak class language, regardless of overflow
}
Out of the stack
Public Function pop () {
if (Empty ($this->data))
return false;
$ret = $this->data[$this->end];
Array_splice ($this->data, $this->end);//The array moves forward one
$this->end--;
return $ret;
}
Fetch stack
Public Function Get_stack () {
return $this->data;
}
Defining a binary conversion function
Public Function transform ($num, $to _num) {
$result =null;//Define Results
while ($num!=0) {
$num _y= $num% $to _num;
$num = $num/$to _num;
$this->push ($num _y);
if ($num < $to _num) {
$this->push (Intval ($num));
$num = 0;
}
}
while (!empty ($this->get_stack ())) {
echo $this->pop ();
}
return true;
}
}
$a =new Stack;
$b =10;//The number of decimal numbers that need to be converted
$c =5;//need to be converted into the system
$result = $a->transform ($b, $c);
?>


below is the result of the execution of 20, test success.
The younger brother is merely shallowly, assumes where is not in place also wants to consult.



On the stack of PHP data structure

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.