Skip to content

Adding the Stack Data Structure #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [Doublylinkedlist](./DataStructures/DoublyLinkedList.php)
* [Node](./DataStructures/Node.php)
* [Singlylinkedlist](./DataStructures/SinglyLinkedList.php)
* [Stack](./DataStructures/Stack.php)

## Graphs
* [Breadthfirstsearch](./Graphs/BreadthFirstSearch.php)
Expand Down Expand Up @@ -102,6 +103,7 @@
* Datastructures
* [Doublylinkedlisttest](./tests/DataStructures/DoublyLinkedListTest.php)
* [Singlylinkedlisttest](./tests/DataStructures/SinglyLinkedListTest.php)
* [Stacktest](./tests/DataStructures/StackTest.php)
* Graphs
* [Breadthfirstsearchtest](./tests/Graphs/BreadthFirstSearchTest.php)
* [Depthfirstsearchtest](./tests/Graphs/DepthFirstSearchTest.php)
Expand Down
85 changes: 85 additions & 0 deletions DataStructures/Stack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* Stack Implementation in PHP
*/
class Stack
{
private array $stack = [];

public function __construct(array $array = [])
{
$this->stack = $array;
}

public function __destruct()
{
unset($this->stack);
}


public function push($data): void
{
array_push($this->stack, $data);
}

public function pop()
{
return array_pop($this->stack);
}

public function peek()
{
return $this->stack[count($this->stack) - 1];
}

public function isEmpty(): bool
{
return empty($this->stack);
}

public function print(): void
{
echo implode(', ', $this->stack);
}

public function __toString(): string
{
return implode(', ', $this->stack);
}

public function length(): int
{
return count($this->stack);
}

public function clear(): void
{
$this->stack = [];
}

public function search($data): int
{
return array_search($data, $this->stack);
}

public function toArray(): array
{
return $this->stack;
}

public function fromArray(array $array): void
{
$this->stack = $array;
}

public function reverse(): void
{
$this->stack = array_reverse($this->stack);
}

public function sort(): void
{
sort($this->stack);
}
}
110 changes: 110 additions & 0 deletions tests/DataStructures/StackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

use PHPUnit\Framework\TestCase;

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../DataStructures/Stack.php';

class StackTest extends TestCase
{
public function testConstruct()
{
$stack = new Stack([1, 2, 3]);
$this->assertEquals([1, 2, 3], $stack->toArray());
}

public function testDestruct()
{
$stack = new Stack([1, 2, 3]);
unset($stack);
$this->expectNotToPerformAssertions();
}

public function testPush()
{
$stack = new Stack();
$stack->push(1);
$stack->push(2);
$stack->push(3);
$this->assertEquals([1, 2, 3], $stack->toArray());
}

public function testPop()
{
$stack = new Stack([1, 2, 3]);
$this->assertEquals(3, $stack->pop());
$this->assertEquals([1, 2], $stack->toArray());
}

public function testPeek()
{
$stack = new Stack([1, 2, 3]);
$this->assertEquals(3, $stack->peek());
}

public function testIsEmpty()
{
$stack = new Stack();
$this->assertTrue($stack->isEmpty());
}

public function testPrint()
{
$stack = new Stack([1, 2, 3]);
$this->expectOutputString("1, 2, 3");
$stack->print();
}

public function testToString()
{
$stack = new Stack([1, 2, 3]);
$this->expectOutputString("1, 2, 3");
echo $stack;
}

public function testLength()
{
$stack = new Stack([1, 2, 3]);
$this->assertEquals(3, $stack->length());
}

public function testClear()
{
$stack = new Stack([1, 2, 3]);
$stack->clear();
$this->assertEquals([], $stack->toArray());
}

public function testSearch()
{
$stack = new Stack([1, 2, 3]);
$this->assertEquals(2, $stack->search(3));
}

public function testToArray()
{
$stack = new Stack([1, 2, 3]);
$this->assertEquals([1, 2, 3], $stack->toArray());
}

public function testFromArray()
{
$stack = new Stack();
$stack->fromArray([1, 2, 3]);
$this->assertEquals([1, 2, 3], $stack->toArray());
}

public function testReverse()
{
$stack = new Stack([1, 2, 3]);
$stack->reverse();
$this->assertEquals([3, 2, 1], $stack->toArray());
}

public function testSort()
{
$stack = new Stack([3, 2, 1]);
$stack->sort();
$this->assertEquals([1, 2, 3], $stack->toArray());
}
}