Run Code
|
Code Wall
|
Users
|
Misc
|
Feedback
|
About
|
Login
|
Theme
|
Privacy
PHP - String Reverse Using Stack Implementation
<?php //php 7.0.8 $string = "hello"; echo reverse($string); function reverse($string) { $stack = new Stack(strlen($string)); for ($i=0; $i<strlen($string); $i++) { $stack->push($string[$i]); } $r = ""; while (!$stack->isempty()) { $r .= $stack->pop(); } return $r; } class Stack { private $size; private $data; private $top; public function __construct($size) { $this->size = $size; $this->data = array(); $this->top = -1; } public function push($element) { if ($this->top == ($this->size-1)) { throw new Exception("Error: Stack is full"); } $this->data[++$this->top] = $element; } public function pop() : string { if ($this->isempty()) { throw new Exception("Error: Stack is empty"); } return $this->data[$this->top--]; } public function top() : string { return $this->data[$this->top]; } public function isempty() : bool { if ($this->top == -1) return true; return false; } } ?>
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Weighted random number generator by gamma parametr, Php - rextester
Akd
Ttrf
Get Max Key in PHP
Problem: binary
Website url validation using php
PHP - BinarySearch (Find-last-occurrence-of-an-element)
Sum of n numbers
Afnan Khan
PHP - BinarySearch (Find-element-in-circular-sorted-array)
Please log in to post a comment.