https://github.com/canciolabs/php-stack
This tiny package contains an interface and an array-based implementation of the LIFO Stack data structure.
https://github.com/canciolabs/php-stack
data-structures php stack
Last synced: about 1 month ago
JSON representation
This tiny package contains an interface and an array-based implementation of the LIFO Stack data structure.
- Host: GitHub
- URL: https://github.com/canciolabs/php-stack
- Owner: canciolabs
- License: gpl-3.0
- Created: 2024-02-25T17:32:12.000Z (about 2 years ago)
- Default Branch: 1.2
- Last Pushed: 2025-01-24T22:53:49.000Z (about 1 year ago)
- Last Synced: 2025-03-27T12:52:32.157Z (about 1 year ago)
- Topics: data-structures, php, stack
- Language: PHP
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CancioLabs / DS / PHP-Stack
This tiny package contains an interface and an array-based implementation of the LIFO Stack data structure.
## Interface
| Method | Description |
|---------|--------------------------------------------------|
| push | Adds a new element to the top of the stack. |
| pop | Removes and return the top element of the stack. |
| top | Returns the top element of the stack. |
| isEmpty | Tests whether the stack is empty. |
| clear | Removes all elements from the stack. |
| count | Returns the number of elements of the stack. |
| toArray | Transforms the stack into an array. |
## How to use it
```
$stack = new Stack(['A', 'B']);
$stack->push('C');
$stack->push('D');
$stack->isEmpty(); // returns false
$stack->count(); // returns 4
$stack->top(); // output 'D'
$stack->pop(); // returns 'D'
$array = $stack->toArray(); // return ['A', 'B', 'C']
foreach ($stack as $element) {
// i=0: $element = 'C'
// i=1: $element = 'B'
// i=2: $element = 'A'
}
$stack->isEmpty(); // returns true
```
## Tests and Coverage
All tests are passing with no warnings and code coverage is 100%.