function slink( p ) 
{ 
	this.next = 0; 
	this.member = (p ==(void 0 ) ? 0 : p); 
}

function slist_base( a ) 
{
	this.count = 0;
	if (a != (void 0 )) { 
		this.last = a;
		a.next = a; 
		this.count = 1; 
	} else
		this.last = 0; 
}

slist_base.prototype.getLast = function()
{
	return( this.last );
}		
		
slist_base.prototype.getFirst = function()
{
	return( this.last != 0 ? this.last.next : 0  );
}

slist_base.prototype.getNext = function( a ) 
{
	return( a.next != this.last.next ? a.next : 0 );
}

slist_base.prototype.prepend = function( a ) 
{
	if( this.last )
		a.next = this.last.next;
	else 
		this.last = a;
	this.last.next = a; 
	this.count++;
	return( a );
}
slist_base.prototype.append = function( a ) 
{ 
	if( this.last ) {
		a.next = this.last.next;
		this.last = this.last.next = a;
	} else 
		this.last = a.next = a;

	this.count += 1;
	return( a );
} 

slist_base.prototype.remove = function( a ) 
{ 
	if( this.last == 0 ) 
		return( 0 );
	var Prev;
	if( a == this.last.next ) 
		Prev = this.last;
	else {
		Prev = a;

		while( Prev.next != a )
			Prev = Prev.next;
	}
	if( Prev == a )
		this.last = 0;
	else {
		Prev.next = a.next;
		if( this.last == a )
			this.last = Prev;
	}
	this.count -= 1;
	return ( a );
}


slist_base.prototype.removeLast = function( )
{
	return( this.remove( this.last ) );
}

slist_base.prototype.insert = function( b ) 
{
	if ( this.compare != (void 0) ) {
		var a = this.getFirst();
		var prv = 0;
		var didit = 0;
		while( a ) {
    		if( this.compare( a.member, b.member ) == 1 ) {
				if( prv ) {
            		prv.next = b;
					b.next = a;
					this.count++;
				} else 
					this.prepend( b );
				
				didit = 1;
				break;
			}
			prv = a;
			a = this.getNext( a );
		}
		if( !didit )
    		this.append( b );
	} else 
		this.prepend( b );

	return( b ); 
}

slist.prototype.dump = function( ) 
{
	a = this.me.last.next;

	while( a ) {
		document.writeln( a.member + "<br>" );
		a = this.me.getNext( a );
	}
}



function slist()
{
	this.me = new slist_base();
}

slist.prototype.push = function( a ) 
{
	this.me.prepend( new slink( a ) );
}

slist.prototype.pop = function()
{
	var r=0;
    var t = this.me.remove( this.me.getFirst() );
	if( t ) 
		r = t.member;
	
	return r;
}

slist.prototype.last = function()
{
	return this.me.getLast().member;
}

slist.prototype.prepend = function( a )
{
	this.me.insert( new slink( a ) );
}

slist.prototype.append = function( a )
{
	this.me.append( new slink( a ) );
}

slist.prototype.getFirst = function(){
	found = this.me.getFirst();
	return( found ? found.member : 0 );
}

slist.prototype.remove = function( a )
{
    p = this.me.getFirst();
    while( p ) {
    	if( p.member == a ) {
        	l = this.me.remove( p );
            r = l.member;
        	return( r );
        }
        p = this.me.getNext( p );
    }
    return( 0 );
}

slist.prototype.count = function()
{
	return this.me.count;
}
slist.prototype.insert = function( a )
{
	return this.me.insert( new slink( a ) );
}

slist_base.prototype.compare = function( a, b )
{
	return a > b ;
}
