How to Implementation of Stack in PHP

View: 361    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 361

Comment:

To comment you must be logged in members.

Files with category

  • SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    View: 114    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout. SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays...

  • How to use the PHP download file script? Class Download PHP

    How to use the PHP download file script? Class Download PHP

    View: 83    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    This is my favorite PHP download script. I’ve used a different more simple method until a client wanted to be able to allow their site visitors to download a large file from a password protected directory. The PHP script works on Apache web servers...

  • Installing OCI8 & PHP 7.2 on RHEL or CentOS 6

    Installing OCI8 & PHP 7.2 on RHEL or CentOS 6

    View: 229    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    You’ll need to stop Apache (web server) using the following command. In any Linux commands I show in this article, it presumes you aren’t logged in as the root user, so you will need to use sudo to issue privileged commands.

  • PHP source code: get content of google search

    PHP source code: get content of google search

    View: 189    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: 128    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: 4200    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: 496    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: 474    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.

 
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