// IMGList.cpp: implementation of the CIMGList class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Morphing.h"
#include "IMGList.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIMGList::CIMGList()
{	
	m_pHead = NULL;
	m_nCount = 0;
}

CIMGList::~CIMGList()
{
	DeleteAllNode();
}

void CIMGList::DeleteAllNode()
{
	IMGNODE		*pNode;
	while (m_pHead != NULL) {
		pNode = m_pHead;
		m_pHead = m_pHead->pNext;
		delete pNode->pIMG;
		delete pNode;
	}
}

void CIMGList::InsertNodeTail(CIMG *pIMG)
{
	// Allocate new node
	IMGNODE		*pNode = new IMGNODE;
	pNode->pIMG = new CIMG;
	pNode->pIMG->CopyIMG(*pIMG);
	pNode->pNext = NULL;
	pNode->pBefore = NULL;

	if (m_pHead == NULL) {				// empty list
		m_pHead = pNode;
		m_pTail = pNode;
	} else {
		IMGNODE *cursor = m_pHead;
		while (cursor->pNext != NULL)
			cursor = cursor->pNext;
		cursor->pNext = pNode;
		pNode->pBefore = cursor;		// double linked list
		m_pTail = pNode;
	}

	m_nCount++;
}

int CIMGList::GetNodeCount()
{
	return m_nCount;
}

IMGNODE * CIMGList::GetHeadPointer()
{
	return m_pHead;
}

IMGNODE * CIMGList::GetTailPointer()
{
	return m_pTail;
}
