Back to home

PHP Linked List

Published: Jan 17, 2019

Last updated: Jan 17, 2019

    This expects an installation on the system of phpunit.

    Test File

    Create linked_list_test.php:

    <?php require "linked-list.php"; class LinkedListTest extends PHPUnit\Framework\TestCase { public function testInsertFirst() { $n1 = new Node(1); $n2 = new Node(2); $ll = new LinkedList($n1); $ll->insertFirst($n2); $this->assertEquals($ll->size, 2); } public function testGetFirst() { $n1 = new Node(1); $n2 = new Node(2); $ll = new LinkedList($n1); $ll->insertFirst($n2); $this->assertEquals($ll->size, 2); $this->assertEquals($ll->getFirst(), $n2); } }

    Linked List

    Create linked_list.php:

    <?php class LinkedList { public $size = 0; public $head; function __construct($head = null) { $this->head = $head; if ($head != null) { $this->size = 1; } else { $this->size = 0; } } public function insertFirst($n) { if ($this->head != null) { $tmp = $this->head; $this->head = $n; $n->setNext($tmp); } else { $this->head = $n; } $this->size++; } public function getFirst() { return $this->head; } } class Node { private $data; private $next; function __construct($data = null) { $this->data = $data; } public function getData() { return $this->data; } public function setData($data) { $this->data = $data; } public function getNext() { return $this->next; } public function setNext($next) { $this->next = $next; } }

    Running Tests

    Change into directory and run phpunit.phar linked_list_test.php.

    Personal image

    Dennis O'Keeffe

    @dennisokeeffe92
    • Melbourne, Australia

    Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
    I am currently working on Visibuild.

    1,200+ PEOPLE ALREADY JOINED ❤️️

    Get fresh posts + news direct to your inbox.

    No spam. We only send you relevant content.

    PHP Linked List

    Introduction

    Share this post