"HR30 Day 15: Linked List/PHP"의 두 판 사이의 차이

 
38번째 줄: 38번째 줄:
     }
     }
}
}
$T=intval(fgets(STDIN));
$T = intval(fgets(STDIN));
$head=null;
$head = null;
$mylist=new Solution();
$mylist = new Solution();
while($T-->0) {
while($T-->0) {
     $data=intval(fgets(STDIN));
     $data = intval(fgets(STDIN));
     $head=$mylist->insert($head,$data);
     $head = $mylist->insert($head,$data);
}
}
$mylist->display($head);
$mylist->display($head);
</source>
</source>

2018년 8월 12일 (일) 08:06 기준 최신판

개요[ | ]

<?php
class Node {
    public $data;
    public $next;
    function __construct($d) {
        $this->data = $d;
        $this->next = NULL;
    }
}
class Solution{
    function insert($head, $data) {
        $newNode = new Node($data);
        if(is_null($head)) {
            return $newNode;
        }
        $node = $head;
        while(!is_null($node->next)) {
            $node = $node->next;
        }
        $node->next = $newNode;
        return $head;
    }
    function display($head) {
        $start=$head;
        while($start){
            echo $start->data,' ';
            $start=$start->next;
        }
    }
}
$T = intval(fgets(STDIN));
$head = null;
$mylist = new Solution();
while($T-->0) {
    $data = intval(fgets(STDIN));
    $head = $mylist->insert($head,$data);
}
$mylist->display($head);
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}