diff --git a/README.md b/README.md index 3898e01..61000e5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: diff --git a/src-php/LinkedList.php b/src-php/LinkedList.php new file mode 100644 index 0000000..81b91c7 --- /dev/null +++ b/src-php/LinkedList.php @@ -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; + } + +} diff --git a/src-php/Node.php b/src-php/Node.php new file mode 100644 index 0000000..d298a0f --- /dev/null +++ b/src-php/Node.php @@ -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; + } + +} + diff --git a/src-php/Queue.php b/src-php/Queue.php new file mode 100644 index 0000000..b4778e5 --- /dev/null +++ b/src-php/Queue.php @@ -0,0 +1,43 @@ +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; + } + +}