How to Implementation of Stack in PHP

View: 311    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Php&mySql   Fields: Other

9 point/1 review File has been tested

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.

Introduction

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 311

Comment:

To comment you must be logged in members.

Files with category

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

    View: 4071    Download: 0   Comment: 0   Author: none  

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

    Category: Php&mySql
    Fields: Other

    1.6666666666667/3 review
    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

    View: 395    Download: 0   Comment: 0   Author: none  

    How to MySqli Tutorial PHP MySqli Extension

    Category: Php&mySql
    Fields: Other

    0/0 review
    PHP provides three api to connect mysql Database.

  • Make Laravel Artisan Commands

    View: 377    Download: 0   Comment: 0   Author: none  

    Make Laravel Artisan Commands

    Category: Php&mySql
    Fields: Other

    0/0 review
    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

    View: 343    Download: 0   Comment: 0   Author: none  

    Check if a Number is a Power of 2

    Category: Php&mySql
    Fields: Other

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

  • Concatenate columns in MySql

    View: 403    Download: 0   Comment: 0   Author: none  

    Concatenate columns in MySql

    Category: Php&mySql
    Fields: Other

    0/2 review
    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

    View: 336    Download: 0   Comment: 0   Author: none  

    How to Query NULL Value in MySql

    Category: Php&mySql
    Fields: Other

    5/1 review
    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...

  • How to Abstract Class in PHP

    View: 374    Download: 0   Comment: 0   Author: none  

    How to Abstract Class in PHP

    Category: Php&mySql
    Fields: Other

    0/0 review
    What is an abstract class in PHP and when to use an abstract class in your application. In this tutorial, we’ll learn about abstract class and their implementation.

  • Use Enums in Rails for Mapped Values

    View: 337    Download: 0   Comment: 0   Author: none  

    Use Enums in Rails for Mapped Values

    Category: Php&mySql
    Fields: Other

    2.5/2 review
    When I worked in a call center, we used to mark cases with different statuses. This allowed upper management to get a handle on where cases stood, what the bottlenecks were and flow of calls. Thankfully it has been a long time since I worked in a...

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd