Pages

Search This Blog

Wednesday 14 August 2013

Implementation Of Queue Class and Pointer Approach

<?php

/*
 *
 * @Author: Zuhair Mirza
 * @First Implementation Of Queue Class and Pointer Approach
 * @Date : 30-July-2013
 *
 */

$q = new Queue();

$q->insert(1);
$q->insert(2);
$q->insert(3);

// 1 2 3
echo $q;

$q->delete();
$q->delete();

// 1
echo $q;

$q->insert(15);
$q->insert('hello');
$q->insert('world');
$q->delete();

// 1 15 "hello"
echo $q;

// -------------------------------------Class Implementation--------------------------

class Item {

    public $data = null;
    public $next = null;
    public $prev = null;

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

}

class Queue {

    protected $_head = null;
    protected $_tail = null;

    public function insert($data) {
        $item = new Item($data);

        if ($this->_head == NULL) {
            $this->_head = $item;
        } else if ($this->_tail == NULL) {
            $this->_tail = $item;
            $this->_head->next = $this->_tail;
            $this->_tail->prev = $this->_head;
        } else {
            $this->_tail->next = $item;
            $item->prev = $this->_tail;
            $this->_tail = $item;
        }
    }

    public function delete() {
        if (isset($this->_head->data)) {

            $temp = $this->_tail;
            $data = $temp->data;

            $this->_tail = $this->_tail->prev;

            if (isset($this->_tail->next))
                $this->_tail->next = null;
            else
                $this->_tail = $this->_head = null;

            return $data;
        }

        return FALSE;
    }

    public function __toString() {
        $output = '';
        $t = $this->_head;
        while ($t) {
            $output .= $t->data . ' | ';
            $t = $t->next;
        }

        return $output;
    }

}

/*
 * ----------------------THE END-----------------------
 */

?>

Tuesday 13 August 2013

Implementation Of Stack Class and Pointer Approach

<?php

/*
 *
 * @Author: Zuhair Mirza
 * @First Implementation Of Stack Class and Pointer Approach
 * @Date : 30-July-2013
 *
 */


$s = new Stack();
$s->push(1);
$s->push(2);
$s->push(3);

// 3 2 1
echo $s;

$s->pop();
$s->pop();

// 1
echo $s;


class Struct {

    protected $_data = null;
    protected $_next = null;

    public function __construct($data, $next) {
        $this->_data = $data;
        $this->_next = $next;
    }

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

}

class Stack {

    protected $_top = null;

    public function push($data) {
        $item = new Struct($data, null);

        if ($this->_top == null) {
            $this->_top = $item;
        } else {
            $item->setNext($this->_top);
            $this->_top = $item;
        }
    }

    public function pop() {
        if ($this->_top) {
            $t = $this->_top;
            $data = $t->getData();

            $this->_top = $this->_top->getNext();

            $t = null;

            return $data;
        }
    }

    public function __toString() {
        $output = '';
        $t = $this->_top;
        while ($t) {
            $output .= $t->getData() . ' ';
            $t = $t->getNext();
        }

        return $output;
    }

}


/*
 * ----------------------THE END-----------------------
 */

?>

Implementation Of Stack Using Array in PHP

<?php
/*
 *
 * @Author: Zuhair Mirza
 * @First Implementation Of Stack Using Array
 * @Date : 30-July-2013
 *
 */

$stack = array();

function push($data, &$stack) {
$stack[] = $data;
}

function pop(&$stack)
{
$len = count($stack);
$top = $stack[$len-1];

unset($stack[$len-1]);

return $top;
}

// array()
print_r($stack);

push(1, $stack);
push(2, $stack);
push('some test', $stack);
push(array(25,12,1999), $stack);

// [1, 2, 'some test', [25, 12, 1999]]
print_r($stack);

// [25, 12, 1999]
echo pop($stack);
// 'some test'
echo pop($stack);

// [1, 2]
print_r($stack);


/*
 * ----------------------THE END-----------------------
 */

?>