HR30 Day 15: Linked List/PHP

Jmnote (토론 | 기여)님의 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 }}