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);




PHP Implementation of stack

During my recent study of data structures I decided to write a few implementations of popular data structures that are not provided by PHP. Below you can find my first implementation of stack data structure.

 

<?php

class stack {

    private $stack = array();

    function push($value) {
        $this->stack[] = $value;
    }

    function pop() {
        if ($this->isEmpty())
        {
            throw new RunTimeException("Stack is empty");
        }
        $top = $this->stack[count($this->stack) - 1];
        unset($this->stack[count($this->stack) - 1]);
        $this->stack = array_values($this->stack);
        return $top;
    }

    function isEmpty() {
        return empty($this->stack) ? true : false;
    }

    function peak() {
        $top = $this->stack[count($this->stack)];
        return $top;
    }

    function printr() {
        print_r($this->stack);
    }

}