How to Implementation of Stack in PHP

View: 328    Dowload: 0   Comment: 0   Post by: hanhga   Category: Php&mySql   Fields: Other

9 point/1 review File has been tested

Stack is a very important data structure in computer science, It works on the principle of last-in-first-out (LIFO ). The element which inserted last is the first element to be popped. In this tutorial, You will learn about stack data structure and how to implement a stack in PHP.

In Stack, insert and delete operations are done from a single end. Insertion and deletion operations are called as push and pop.

Push – To insert an element in a Stack.
Pop – To delete an element from a Stack.

Before any push operation in a Stack, we need to check whether there is a sufficient space available for a new element. If memory is not available then we throw an exception of Stack Overflow. Similarly in pop operation, if a stack is empty then we throw an exception of  Stack Underflow.

Implementation of Stack in PHP

We have learned about a stack and it’s basic operations. Let’s implement them in PHP.

<?php 
class Stack
{
protected $stack;
protected $size;
public function __construct($size = 50) {
//Initialize stack
$this->stack = array();
//Initialize stack size
$this->size  = $size;
}
/**
* Insert an element in a stack
* @param type $data
*/
public function push($data) {
//Check stack overflow
if(count($this->stack) < $this->size) {
//Inserts an element at the beginning
array_unshift($this->stack, $data);
} else {
// If stack is full then throw stack overflow exception
throw new RuntimeException("Stack overflow");
}
}
/**
* Removes an element from a stack
*
*/
public function pop() {
// If stack is empty
if (empty($this->stack)) {
throw new RuntimeException("Stack underflow");
} else {
return array_shift($this->stack);
}
}
}
$stack = new Stack();
$stack->push(4);
$stack->push(5);
echo $stack->pop();  // Pop 5
$stack->push(7);
$stack->push(9);
$stack->push(8);
echo $stack->pop();  // Pop 8
echo $stack->pop();  // Pop 9

Application of Stack Data Structure

i) When functions are called.

ii) In recursion.

iii) Evaluation and conversion of expressions

 

How to Implementation of Stack in PHP

How to Implementation of Stack in PHP Posted on 20-09-2016  A Stack is a very important data structure in computer science, It works on the principle of last-in-first-out (LIFO ). The element which inserted last is the first element to be popped. In this tutorial, You will learn about stack data structure and how to implement a stack in PHP. 9/10 328

Comment:

To comment you must be logged in members.

Files with category

  • PHP source code: get content of google search

    PHP source code: get content of google search

    View: 84    Download: 2   Comment: 0

    Category: Php&mySql     Fields: Other

    How to get the results of google search and display on your website. This PHP code can do that. Refer to the following code.

  • PHP as a Scripting Language for C#

    PHP as a Scripting Language for C#

    View: 58    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    When creating .NET applications (including desktop and web applications), it may be useful to allow extending the application using some scripting language. The users of the application can write simple scripts to configure the application, modify...

  • How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    View: 4125    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    With the explosion of machine learning services in recent years, it has become easier than ever for developers to create “smart apps”. In this article, I’ll introduce you to Microsoft’s offering for providing machine-learning capabilities to apps.

  • How to MySqli Tutorial PHP MySqli Extension

    How to MySqli Tutorial PHP MySqli Extension

    View: 432    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    PHP provides three api to connect mysql Database.

  • Make Laravel Artisan Commands

    Make Laravel Artisan Commands

    View: 409    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow.

  • Check if a Number is a Power of 2

    Check if a Number is a Power of 2

    View: 364    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    How to check if a number is a power of 2. To understand this question, let’s take some example.

  • Concatenate columns in MySql

    Concatenate columns in MySql

    View: 431    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow

  • How to Query NULL Value in MySql

    How to Query NULL Value in MySql

    View: 363    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd