// ListNode.cpp: interface and implementation of the ListNode class.
//
/////////////////////////////////////////////////////////////////////////////////

#include "stdhdrs.h"
#include "strbuf.h"
#include "listnode.h"

CBaseEntity* ListList :: EPop()
{
	if (head == NULL) return NULL;
	ListNode* old = head;
	CBaseEntity* value = head->ent;
	if (head->next) head = head->next;
	else 
	{
		head = NULL;
		tail = NULL;
	}
	delete old;
	return value;
}

StrBuf ListList :: SPop()
{
	if (head == NULL) return StrBuf();
	ListNode* old = head;
	StrBuf value = head->buf;
	if (head->next) head = head->next;
	else 
	{
		head = NULL;
		tail = NULL;
	}
	delete old;
	return value;
}

void ListList :: EKill(CBaseEntity* target)
{
	if (head == NULL) return;
	ListNode* seek = head;
	if (seek->ent == target)
	{
		head = seek->next;
		delete seek;
		return;
	}
	ListNode* prev = seek;
	seek = seek->next;
	while (seek != NULL)
	{
		if (seek->ent == target)
		{
			prev->next = seek->next;
			delete seek;
			return;
		}
		prev = seek;
		seek = seek->next;
	}
}


void ListList::Append(CBaseEntity* ent)
{
	if (head == NULL) {
		head = new ListNode(ent);
		tail = head;
	}
	else
	{
		tail->next = new ListNode(ent);
		tail = tail->next;
	}
}

void ListList::Append(StrBuf buf)
{
	if (head == NULL) {
		head = new ListNode(buf);
		tail = head;
	}
	else
	{
		tail->next = new ListNode(buf);
		tail = tail->next;
	}
}

ListList::ListList()
{
	head = NULL;
	tail = NULL;
}

ListList::~ListList()
{
	if (head) head->Clean();
}

ListNode::ListNode()
{
	next = NULL;
}

ListNode::ListNode(CBaseEntity* tent)
{
	ent = tent;
	next = NULL;
}

ListNode::ListNode(StrBuf tbuf)
{
	buf = StrBuf();
	buf.Append(tbuf.Text());
	next = NULL;
}

ListNode::~ListNode()
{

}

void ListNode::Clean()
{
	if(next) next->Clean();
	delete this;
}
