PHP で、LinkedList を実装する

基本構造

  1. Node クラスと LinkedList クラスの 2つを作成
  2. Node クラス→ $data$next の 2つのメンバー変数、このうち $dataprivate で宣言、アクセス用のメソッド function data() を用意
  3. LinkedList クラスは $first メンバー変数のみ、$firstNode オブジェクトをキープする。あとに続くすべてのノードの起点となる。
    insert
    =====
  4. リストの最後に追加するノード $lastnew Node($data) で新規作成
  5. $this->firstNULL の場合を判定、NULL だったら $this->first$last を代入(この状態ではノードが1つしかなく、最初のノードが最後のノードと同義だから)
  6. $current を一時的なノードを表す変数としてまずは $this->first を代入してノードの先頭から始め、
  7. $current->next != NULL になるまで while をまわす
  8. while が終わったら、最後は $node->next$last を代入
  9. $lastreturn する
    delete
    =====
  10. $this->first と一致した場合、$this->firstNULL にする
  11. $current を一時的なノードを表す変数としてまずは $this->first を代入してノードの先頭から始め、
  12. $current->next != NULL になるまで while をまわす
  13. もし $current->next == $current の場合
    5. $current->next = $current->next->next にして現在の $current をとばす。そのまま return して while を抜ける
  14. $current を次のノード $current->next にして while に戻る$current = $current->next`)
    read
    =====
  15. array() を用意(この場合は $list 変数)
  16. $current を一時的なノードを表す変数としてまずは $this->first を代入して
  17. ノードの先頭から始め、$current != NULL になるまで while をまわす
  18. array_push($list, $current->data());
  19. $current に次のノード $current->next を代入
  20. foreach$list as $valueprint $value する
    コード
    =====
<?php

class Node {

  private $data;
  public  $next;

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

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

class LinkedList {

  private $first = NULL;

  function __construct() {

    $this->first = NULL;
  }

  public function insert($data) {

    $last = new Node($data);
    if($this->first == NULL) {
      // if the number of node = 0
      return $this->first = $last;
    }   

    $current = $this->first;
    // if count($node) > 1 
    while($current->next != NULL) {
      $current = $current->next;
    }
    // if the number of node >= 1
    $current->next = &$last;
    return $last;
  }

  public function delete($node) {

    if($node == $this->first) {
      $this->first = NULL;
      return;
    }

    $current = $this->first;
    while($current->next != NULL) {
      if($current->next == $node) {
        $current->next = $current->next->next;
        return;
      }
      $current = $current->next;
    }
  }

  public function read() {
    $list = array();
    $current = $this->first;
    while($current != NULL) {
      array_push($list, $current->data());
      $current = $current->next;
    }

    foreach($list as $value)
      print "$value ";
  }
}

$linkedlist = new LinkedList();

print "step 1: ";
$node1 = $linkedlist->insert(1);
print $node1->data() . "\n";

print "step 2: ";
$node2 = $linkedlist->insert(2);
print $node2->data() . "\n";

print "step 3: ";
$node3 = $linkedlist->insert(3);
print $node3->data() . "\n";

print "step 4: ";
$node4 = $linkedlist->insert(4);
print $node4->data() . "\n";

print "step 5: ";
$linkedlist->delete($node3);
print "deleted node3\n";

print "\nValues -----\n";
$linkedlist->read();
print "\n";

出力結果

step1: 1
step 2: 2
step 3: 3
step 4: 4
step 5: deleted node3

Values -----
1 2 4