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

(새 문서: 분류: 30 Days of Code ==개요== * HR30 Day 15: Linked List <source lang='php'> <?php class Node{ public $data; public $next; function __construct($d) {...)
 
5번째 줄: 5번째 줄:
<source lang='php'>
<source lang='php'>
<?php
<?php
class Node{
class Node {
     public $data;
     public $data;
     public $next;
     public $next;
     function __construct($d)
     function __construct($d) {
    {
         $this->data = $d;
         $this->data = $d;
         $this->next = NULL;
         $this->next = NULL;
17번째 줄: 16번째 줄:
</source>
</source>
<source lang='php'>
<source lang='php'>
     function insert($head, $data){
     function insert($head, $data) {
         $newNode = new Node($data);
         $newNode = new Node($data);
         if(is_null($head)) {
         if(is_null($head)) {
31번째 줄: 30번째 줄:
</source>
</source>
<source lang='php'>
<source lang='php'>
function display($head){
    function display($head) {
         $start=$head;
         $start=$head;
         while($start){
         while($start){
42번째 줄: 41번째 줄:
$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 }}