Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
40.00% |
2 / 5 |
CRAP | |
73.91% |
17 / 23 |
Stack | |
0.00% |
0 / 1 |
|
40.00% |
2 / 5 |
9.14 | |
73.91% |
17 / 23 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
isEmpty | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
push | |
0.00% |
0 / 1 |
2.35 | |
55.56% |
5 / 9 |
|||
pop | |
0.00% |
0 / 1 |
2.01 | |
85.71% |
6 / 7 |
|||
peek | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
<?php | |
namespace Mtkocak\DataStructures; | |
class Stack | |
{ | |
private $top; | |
private $bottom; | |
function __construct() | |
{ | |
$this->top = NULL; | |
$this->bottom = NULL; | |
} | |
public function isEmpty() | |
{ | |
if ($this->top == NULL) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function push($value){ | |
$newNode = new SingleLinkedListNode($value); | |
if($this->isEmpty()){ | |
$this->top = $newNode; | |
$this->tbottom = $newNode; | |
return true; | |
}else{ | |
$oldTop = $this->top; | |
$this->top = $newNode; | |
$newNode->next($oldTop); | |
return true; | |
} | |
return false; | |
} | |
public function pop(){ | |
if(!$this->isEmpty()){ | |
$top = $this->top; | |
$value = $top->get(); | |
$this->top = $top->next(); | |
unset($top); | |
return $value; | |
}else{ | |
return false; | |
} | |
} | |
public function peek(){ | |
return $this->top->get(); | |
} | |
} |