Skip to content

Commit

Permalink
clean code queue and stack
Browse files Browse the repository at this point in the history
  • Loading branch information
kenziehong committed Feb 13, 2020
1 parent eabcd57 commit 02bf990
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 103 deletions.
36 changes: 14 additions & 22 deletions Basic/DataStructure/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,46 @@ public function enqueue($data) {
$newNode = new Node($data);

if ($this->isEmpty()) {
$this->setTailNode($newNode); // differ
$this->setTailNode($newNode);
} else {
$headNode->setNextNode($newNode); // $tailNode->setNextNode
$headNode->setNextNode($newNode);
}

$this->setHeadNode($newNode);

$this->_size += 1;
}
}

public function dequeue() {
if ($this->isEmpty()) {
return null;
}

$tailNode = $this->getTailNode();

if ($tailNode) {
$nextNode = $tailNode->getNextNode();
$tailValue = $tailNode->getData();
$tailData = $tailNode->getData();

$tailNode->setNextNode($nextNode);
$this->setTailNode($nextNode);
$this->_size -= 1;

return $tailValue;
return $tailData;
}
}

public function peek() {
$currentNode = $this->getHeadNode();
$tailNode = $this->getTailNode();

if($currentNode) {
$currentData = $currentNode->getData();
return $currentData;
if($tailNode) {
$tailData = $tailNode->getData();
return $tailData;
}
}

public function traverse() {
$strqueue = '';
$currentNode = $this->getTailNode();
$tailNode = $this->getTailNode();

while ($currentNode) {
$strqueue .= $currentNode->getData() . ' -> ';
$currentNode = $currentNode->getNextNode();
while ($tailNode) {
$strqueue .= $tailNode->getData() . ' -> ';
$tailNode = $tailNode->getNextNode();
}

echo $strqueue;
Expand All @@ -95,10 +91,6 @@ public function isEmpty() {
return $this->_size === 0;
}

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

}

$queue = new Queue(5);
Expand Down
72 changes: 0 additions & 72 deletions Basic/DataStructure/Queue2.php

This file was deleted.

18 changes: 9 additions & 9 deletions Basic/DataStructure/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ public function pop() {
}

public function peek() {
$currentNode = $this->getHeadNode();
$headNode = $this->getHeadNode();

if($currentNode) {
$currentData = $currentNode->getData();
return $currentData;
if($headNode) {
$headData = $headNode->getData();
return $headData;
}
}

public function traverse() {
$strStack = '';
$currentNode = $this->getHeadNode();
$headNode = $this->getHeadNode();

while ($currentNode) {
$strStack .= $currentNode->getData() . ' -> ';
$currentNode = $currentNode->getNextNode();
while ($headNode) {
$strStack .= $headNode->getData() . ' -> ';
$headNode = $headNode->getNextNode();
}

echo $strStack;
Expand All @@ -85,7 +85,7 @@ public function hasSpace() {

$stack->traverse();
echo PHP_EOL;
echo $stack->peek();
echo $stack->pop();
echo PHP_EOL;
$stack->traverse();

Expand Down

0 comments on commit 02bf990

Please sign in to comment.