Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This contains all the programs for data structures that are a part of the syllab
- [Java](https://github.com/diptangsu/Data-Structures/blob/master/src-java)
- [Javascript](https://github.com/diptangsu/Data-Structures/blob/master/src-javascript)
- [Python](https://github.com/diptangsu/Data-Structures/blob/master/src-python) :snake:
- [Php](https://github.com/diptangsu/Data-Structures/blob/master/src-php)

---
# Stack
Expand Down Expand Up @@ -62,6 +63,10 @@ A Queue is a linear structure which follows a particular order in which the oper
- Inserting the new item at the beginning of the Queue.
- Dequeue()
- Taking out the last item of the Queue.
- Peek()
- Get the last item of the Queue without remove it.
- IsEmpty()
- Verify if Queue is empty.

---
# Tree :deciduous_tree:
Expand Down
178 changes: 178 additions & 0 deletions src-php/LinkedList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
namespace srcPhp;

use srcPhp\Node;

class LinkedList implements \Iterator {

private $_firstNode = NULL;
private $_lastNode = NULL;
private $_totalNode = 0;
private $_currentNode = NULL;
private $_currentPosition = 0;

public function insertLast(string $data = NULL) {
$newNode = new Node($data);
if ($this->_firstNode === NULL) {
$this->_firstNode = &$newNode;
$this->_lastNode = &$newNode;
} else {
$currentNode = $this->_lastNode;
$currentNode->next = $newNode;
$newNode->prev = $currentNode;
$this->_lastNode = &$newNode;
}
$this->_totalNode++;
return TRUE;
}

public function insertFirst(string $data = NULL) {
$newNode = new Node($data);
if ($this->_firstNode === NULL) {
$this->_firstNode = &$newNode;
$this->_lastNode = &$newNode;
} else {
$currentFirstNode = $this->_firstNode;
$this->_firstNode = &$newNode;
$newNode->next = $currentFirstNode;
$this->_lastNode = &$currentFirstNode;
}
$this->_totalNode++;
return TRUE;
}

public function insertAt(string $data = NULL, string $index = NULL) {
$newNode = new Node($data);

if ($this->_firstNode) {
$previous = NULL;
$currentNode = $this->_firstNode;
while ($currentNode !== NULL) {
if ($currentNode->data === $index) {
$newNode->next = $currentNode;
if ($previous === NULL) {
$this->_firstNode = &$newNode;
} else {
$previous->next = $newNode;
}
$this->_totalNode++;
break;
}
$previous = $currentNode;
$currentNode = $currentNode->next;

}
}
}

public function removeFirst() {
if ($this->_firstNode !== NULL) {
if ($this->_firstNode->next !== NULL) {
$this->_firstNode = $this->_firstNode->next;
} else {
$this->_firstNode = NULL;
$this->_lastNode = NULL;
}
$this->_totalNode--;
return TRUE;
}
return FALSE;
}

public function removeLast() {
if ($this->_firstNode !== NULL) {
$currentNode = $this->_firstNode;
if ($currentNode->next === NULL) {
$this->_firstNode = NULL;
} else {
$previousNode = NULL;

while ($currentNode->next !== NULL) {
$previousNode = $currentNode;
$currentNode = $currentNode->next;
}

$previousNode->next = NULL;
$this->_lastNode = &$previousNode;
$this->_totalNode--;
return TRUE;
}
}
return FALSE;
}

public function removeAt(string $index = NULL) {
if ($this->_firstNode) {
$previous = NULL;
$currentNode = $this->_firstNode;
while ($currentNode !== NULL) {
if ($currentNode->data === $index) {
if ($currentNode->next === NULL) {
$previous->next = NULL;
} else {
$previous->next = $currentNode->next;
}

if($currentNode === $this->_lastNode) {
$this->_lastNode = &$previous;
}

$this->_totalNode--;
break;
}
$previous = $currentNode;
$currentNode = $currentNode->next;
}
}
}

public function getFirst() {
if ($this->_firstNode !== NULL)
return $this->_firstNode->data;
}

public function getLast() {
if ($this->_lastNode !== NULL)
return $this->_lastNode->data;
}

public function getAt(int $n = 0) {
$count = 1;
if ($this->_firstNode !== NULL && $n <= $this->_totalNode) {
$currentNode = $this->_firstNode;
while ($currentNode !== NULL) {
if ($count === $n) {
return $currentNode;
}
$count++;
$currentNode = $currentNode->next;
}
}
}

public function getSize() {
return $this->_totalNode;
}

public function current() {
return $this->_currentNode->data;
}

public function next() {
$this->_currentPosition++;
$this->_currentNode = $this->_currentNode->next;
}

public function key() {
return $this->_currentPosition;
}

public function rewind() {
$this->_currentPosition = 0;
$this->_currentNode = $this->_firstNode;
}

public function valid() {
return $this->_currentNode !== NULL;
}

}
14 changes: 14 additions & 0 deletions src-php/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace srcPhp;

class Node {

public $data = NULL;
public $next = NULL;
public $prev = NULL;

public function __construct(string $data = NULL) {
$this->data = $data;
}

}

43 changes: 43 additions & 0 deletions src-php/Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace srcPhp;

use srcPhp\LinkedList;

class Queue {

private $n;
private $queue;

public function __construct(int $n = 20) {
$this->n = $n;
$this->queue = new LinkedList();
}

public function dequeue(): string {
if ($this->isEmpty()) {
throw new UnderflowException('Queue is empty');
} else {
$lastItem = $this->peek();
$this->queue->removeFirst();
return $lastItem;
}
}

public function enqueue(string $newItem) {
if ($this->queue->getSize() < $this->n) {
$this->queue->insertLast($newItem);
} else {
throw new OverflowException('Queue is full');
}
}

public function peek(): string {
return $this->queue->getAt(1)->data;
}

public function isEmpty(): bool {
return $this->queue->getSize() == 0;
}

}