PHP implementation stack (stack) data structure _php tutorial

Source: Internet
Author: User
Stack (stack), is a special kind of LIFO table, which can only be inserted at one end (usually referred to as a stack, stack or stack) and delete (delete generally referred to as the stack, stack or stack) operation, allowing the insertion and deletion of the end of the operation is called the top of the stack, the other end is called the bottom. Stack, according to the principle of last-in-first-out to store data, the first entry of the data is pressed into the bottom of the stack, and then enter the data at the top of the stack, need to read the data, from the top of the stack to eject data. When there are no elements in the stack, it is called an empty stack.

Data structure and algorithm (PHP implementation)-Stack (stack) 1
/**
* Data structure and algorithm (PHP implementation)-Stack (stack).
*
* @author Gen Want programming (topphp.org)
* @copyright Copyright (c) 2013 Gen want Programming (topphp.org) All rights Reserved
* @license http://www.opensource.org/licenses/mit-license.php mit License
* @version 1.0.0-build20130607
*/
Class Stack {
/**
Stack
*
* @var Array
*/
Private $stack;

/**
* The length of the stack.
*
* @var Integer
*/
Private $size;

/**
* Construction Method-initializes the data.
*/
Public Function __construct () {
$this->stack = Array ();
$this->size = 0;
}

/**
* Press stack (into the stack, into the stack) operation.
*
* @param mixed $data Press stack data.
* @return Object returns the objects themselves.
*/
Public function push ($data) {
$this->stack[$this->size++] = $data;

return $this;
}

/**
* Stack (rewind, stack) operation.
*
* Returns false @return mixed empty stack, otherwise returns the top element of the stack.
*/
Public Function pop () {
if (! $this->isempty ()) {
$top = Array_splice ($this->stack,--$this->size, 1);

return $top [0];
}

return FALSE;
}

/**
* Get stacks.
*
* @return Array returns the stack.
*/
Public Function Getstack () {
return $this->stack;
}

/**
* Gets the top element of the stack.
*
* Returns false @return mixed empty stack, otherwise returns the top element of the stack.
*/
Public Function GetTop () {
if (! $this->isempty ()) {
return $this->stack[$this->size-1];
}

return FALSE;
}

/**
* Gets the length of the stack.
*
* @return Integer Returns the length of the stack.
*/
Public Function GetSize () {
return $this->size;
}

/**
* Check whether the stack is empty.
*
* @return Boolean empty stack returns True, otherwise false is returned.
*/
Public Function IsEmpty () {
return 0 = = = $this->size;
}
}
?>

Example code 1
$stack = new stack ();
$stack->push (1)->push (2)->push (3)->push (4)->push (5)->push (6);
Echo '

', Print_r ($stack->getstack (), TRUE), '
';

$stack->pop ();
Echo '
', Print_r ($stack->getstack (), TRUE), '
';
?>

Description: PHP array functions already have function functions similar to stacks: Array_push (Stack) and, Array_pop (stack).

http://www.bkjia.com/PHPjc/477269.html www.bkjia.com true http://www.bkjia.com/PHPjc/477269.html techarticle stack (stack), is a special kind of LIFO table, which can only be inserted at one end (insert is generally referred to as the stack, the stack or into the stack) and delete (delete commonly known as the stack, fallback ...)

  • 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.