HR30 Day 15: Linked List/PHP

Jmnote (토론 | 기여)님의 2018년 8월 12일 (일) 08:05 판 (새 문서: 분류: 30 Days of Code ==개요== * HR30 Day 15: Linked List <source lang='php'> <?php class Node{ public $data; public $next; function __construct($d) {...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

개요

<?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 }}