-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListStack.php
More file actions
43 lines (35 loc) · 909 Bytes
/
LinkedListStack.php
File metadata and controls
43 lines (35 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace Zubs\Dsa\Stack;
use OverflowException, UnderflowException;
use Zubs\Dsa\LinkedList\LinearLinkedList;
/**
* An implementation of the Stack data structure using LinearLinkedList
*/
class LinkedListStack extends Base
{
public function __construct()
{
$this->stack = new LinearLinkedList();
}
public function push(string $data): bool
{
return $this->stack->insert($data);
}
public function pop(): string
{
if ($this->isEmpty()) throw new UnderflowException('Stack is empty');
else {
$last_item = $this->top();
$this->stack->deleteLast();
return $last_item;
}
}
public function top(): string
{
return $this->stack->getNthNode($this->stack->getSize())->data;
}
public function isEmpty(): bool
{
return $this->stack->getSize() === 0;
}
}