PHP Implementation of Singly Linked List

<?php

class listNode {

    function __construct($data) {
        $this->data = $data;
        $this->next = null;
    }

}

class linkedList {

    function __construct() {

        $this->head = NULL;
    }

    function insert($data) {
        $node = new listNode($data);
        $node->next = $this->head;
        $this->head = $node;
        //if ()
    }

}

class reverse {

    function reversing($obj) {

        $current = $obj->head;
        print_r($current);
        $previous = null;
        $a = 0;
        while ($current != null) {
            echo $a++;
            $head = $current->next;
            $current->next = $previous;
            $previous = $current;
            $current = $head;
        }
        $obj1 = $previous;
        print_r($obj1);
    }

}

$myLinkedList = new linkedList();
$myLinkedList->insert(1);
$myLinkedList->insert(2);
$myLinkedList->insert(3);
$myLinkedList->insert(4);
$myLinkedList->insert(5);
$myLinkedList->insert(6);
//$myLinkedList->insertBegin(4);
echo "Reverse<br>";
$myReverse = new reverse();
$myReverse->reversing($myLinkedList);




Leave a Reply

Your email address will not be published. Required fields are marked *