/**
 * @author hedgehog
 */
function LinkedList(list){
	var _head={head:null};
	var _tail={tail:null};
	var _this=this;
	var _indexes={};

    _head.tail=_tail;_tail.head=_head;
    
    this.serialise=function(){
		var _node=_head.tail;
		var arr=[];

		while(_node!=_tail){
			arr.push(_node.obj);
			_node=_node.tail;
		};
        
        return arr;
	};

    this.remove=function(index){
        _indexes[index].remove();
    };

    this.new_list_item=function(obj){
		var _new_node={
			head:_tail.head,
			tail:_tail,
			obj:obj
		};
		
		_tail.head.tail=_new_node;
		_tail.head=_new_node;

        var yyy=new function(){
			var _this=this;
			
			this.remove=function(){
				var n=_new_node.head;
				_new_node.head.tail=_new_node.tail;
				_new_node.tail.head=n;
				
				_new_node.head=null;
				_new_node.tail=null;
				
				_this.remove=function(){};
				return _new_node.obj;
			};
			
			this.value=function(value){
				if(value) _new_node.obj=value;
				return _new_node.obj; 
			};
		};
        return yyy;
    };

    this.iterator=function(){
        return new function(e){
            var node=_head.tail;
            this.hasNext=function(){
                return node.tail!=null; 
            };

            this.next=function(){
                var _new_node=node;

                node=node.tail;

                return new function(){
                    var _this=this;

                    this.remove=function(){
                        var n=_new_node.head;
                        _new_node.head.tail=_new_node.tail;
                        _new_node.tail.head=n;

                        _new_node.head=null;
                        _new_node.tail=null;

                        _this.remove=function(){};
                        return _new_node.obj;
                    };

                    this.value=function(value){
                        if(value) _new_node.obj=value;
                        return _new_node.obj;
                    };
                };
            };
        };
    };

    if(list){
        _indexes={};
        $.each(list,function(i,n){
            _indexes[i]=_this.new_list_item(n);
		});
	};
};