Home  >  Article  >  Backend Development  >  PHP implements stack (Stack) data structure_PHP tutorial

PHP implements stack (Stack) data structure_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:53856browse

Stack is a special last-in-first-out linear table, which can only be inserted at one end (insertion is generally called push, push or push) and deletion (deletion is generally called stack pop, The end of the stack that allows insertion and deletion operations is called the top of the stack, and the other end is called the bottom of the stack. The stack stores data according to the last-in-first-out principle. The data that enters first is pushed to the bottom of the stack, and the data that enters later is on the top of the stack. When data needs to be read, data is popped from the top of the stack. 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.
*
* @author Chuangxiang Programming (TOPPHP.ORG)
* @copyright Copyright (c) 2013 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 - initialize data.
​*/
public function __construct() {
$this->stack = array();
$this->size = 0;
}

/**
* Push (push into stack, push into stack) operation.
*
* @param mixed $data Push data onto the stack.
* @return object Returns the object itself.
​*/
public function push($data) {
$this->stack[$this->size++] = $data;

Return $this;
}

/**
* Stack popping (ejecting, popping) operations.
*
* @return mixed Returns FALSE when the stack is empty, 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 the stack.
*
* @return array Return stack.
​*/
public function getStack() {
Return $this->stack;
}

/**
* Get the top element of the stack.
*
* @return mixed Returns FALSE when the stack is empty, otherwise returns the top element of the stack.
​*/
public function getTop() {
If (!$this->isEmpty()) {
Return $this->stack[$this->size - 1];
}

Return FALSE;
}

/**
* Get the length of the stack.
*
* @return integer The length of the return stack.
​*/
public function getSize() {
Return $this->size;
}

/**
* Check whether the stack is empty.
*
* @return boolean Returns TRUE if the stack is empty, otherwise returns FALSE.
​*/
public function isEmpty() {
Return 0 === $this->size;
}
}
?>

Sample 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), '
';
?>

Note: PHP array functions already have stack-like functions: array_push (push) and array_pop (pop).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477269.htmlTechArticleStack is a special LIFO linear table that can only be inserted at one end ( Insertion is generally called push, push or push) and deletion (deletion is generally called pop, pop...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn