<?php

include_once('classes/utils/Elem.class.php');
include_once('classes/core/CoreObject.class.php');

class Deque extends CoreObject {

	var $root;
		
	function Deque() {
		$null = null;
		$this->root = &new Elem($null, $null, $null);
		$this->root->setPrev($this->root);
		$this->root->setNext($this->root);		
	}

	function &getRoot() {
		return $this->root;
	}

	function &getTail() {
		$root = &$this->getRoot();
		return $root->getPrev();
	}
		
	function insert(&$object, &$prev, &$next) {
		$elem = &new Elem($object, $prev, $next);
		
		if ($prev != null) {
			$prev->setNext($elem);
    }

		if ($next != null) {
			$next->setPrev($elem);
    }
	}

	function push(&$object) {
	}
	
	function &pop() {
		$root = &$this->getRoot();
		$elem = &$root->getNext();
		
		//detect root element
		if ($elem->getValue() == null) {
			return null;
    }

		$next = &$elem->getNext();

		if ($next != null) {
			$next->setPrev($root);
    }

		$root->setNext($next);
		
		return $elem;
	}
		
}
?>
