⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringlist.cpp

📁 Windows Mobile 手机短信订购软件源程序
💻 CPP
字号:
// StringList.cpp: implementation of the CStringList class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "StringList.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CStringList::CStringList()
{
	m_nCount = 0;
	m_pNodeHead = m_pNodeTail = NULL;
}

void CStringList::RemoveAll()
{
	ATLASSERT(this);
	// destroy elements
	CNode* pNode;
	while(m_pNodeHead != m_pNodeTail)
	{
		pNode = m_pNodeTail;
		m_pNodeTail = m_pNodeTail->pPrev;
		delete pNode;
	}
	delete m_pNodeHead;

	m_nCount = 0;
	m_pNodeHead = m_pNodeTail = NULL;
}

CStringList::~CStringList()
{
	RemoveAll();
	ASSERT(m_nCount == 0);
}

void CStringList::FreeNode(CStringList::CNode* pNode)
{
	delete pNode;
	m_nCount--;
	ASSERT(m_nCount >= 0);	// make sure we don't underflow
}

/////////////////////////////////////////////////////////////////////////////

POSITION CStringList::AddHead(LPCTSTR newElement)
{
	return AddHead(CString(newElement));
}

POSITION CStringList::AddHead(const CString& newElement)
{
	ATLASSERT(this);

	CNode* pNewNode = new CNode;
	ASSERT(pNewNode != NULL);
	if(pNewNode == NULL)
		return NULL;
	pNewNode->pNext = pNewNode->pPrev = NULL;
	
	m_nCount ++;
	pNewNode->data = newElement;
	if (m_pNodeHead != NULL)
	{
		m_pNodeHead->pPrev = pNewNode;
		pNewNode->pNext = m_pNodeHead;
	}
	else
		m_pNodeTail = pNewNode;
	m_pNodeHead = pNewNode;
	return (POSITION) pNewNode;
}

POSITION CStringList::AddTail(LPCTSTR newElement)
{
	return AddTail(CString(newElement));
}

POSITION CStringList::AddTail(const CString& newElement)
{
	ATLASSERT(this);
	CNode* pNewNode = new CNode;
	ASSERT(pNewNode != NULL);
	if(pNewNode == NULL)
		return NULL;
	pNewNode->pNext = pNewNode->pPrev = NULL;
	m_nCount ++;
	
	pNewNode->data = newElement;
	
	if (m_pNodeTail != NULL)
	{
		m_pNodeTail->pNext = pNewNode;
		pNewNode->pPrev = m_pNodeTail;
	}
	else
		m_pNodeHead = pNewNode;
	m_pNodeTail = pNewNode;
	return (POSITION) pNewNode;
}

void CStringList::AddHead(CStringList* pNewList)
{
	ATLASSERT(this);
	ASSERT(pNewList != NULL);
	ATLASSERT(pNewList);

	// add a list of same elements to head (maintain order)
	POSITION pos = pNewList->GetTailPosition();
	while (pos != NULL)
		AddHead(pNewList->GetPrev(pos));
}

void CStringList::AddTail(CStringList* pNewList)
{
	ATLASSERT(this);
	ASSERT(pNewList != NULL);
	ATLASSERT(pNewList);

	// add a list of same elements
	POSITION pos = pNewList->GetHeadPosition();
	while (pos != NULL)
		AddTail(pNewList->GetNext(pos));
}

CString CStringList::RemoveHead()
{
	ATLASSERT(this);
	ASSERT(m_pNodeHead != NULL);  // don't call on empty list !!!

	CNode* pOldNode = m_pNodeHead;
	CString returnValue = pOldNode->data;

	m_pNodeHead = pOldNode->pNext;
	if (m_pNodeHead != NULL)
		m_pNodeHead->pPrev = NULL;
	else
		m_pNodeTail = NULL;
	FreeNode(pOldNode);
	return returnValue;
}

CString CStringList::RemoveTail()
{
	ATLASSERT(this);
	ASSERT(m_pNodeTail != NULL);  // don't call on empty list !!!

	CNode* pOldNode = m_pNodeTail;
	CString returnValue = pOldNode->data;

	m_pNodeTail = pOldNode->pPrev;
	if (m_pNodeTail != NULL)
		m_pNodeTail->pNext = NULL;
	else
		m_pNodeHead = NULL;
	FreeNode(pOldNode);
	return returnValue;
}

POSITION CStringList::InsertBefore(POSITION position, LPCTSTR newElement)
{
	return InsertBefore(position, CString(newElement));
}

POSITION CStringList::InsertBefore(POSITION position, const CString& newElement)
{
	ATLASSERT(this);

	if (position == NULL)
		return AddHead(newElement); // insert before nothing -> head of the list

	// Insert it before position
	CNode* pOldNode = (CNode*) position;
	CNode* pNewNode = new CNode;
	ASSERT( pNewNode != NULL );

	m_nCount ++;
	pNewNode->data = newElement;

	if (pOldNode->pPrev != NULL)
	{
		pOldNode->pPrev->pNext = pNewNode;
		pNewNode->pPrev = pOldNode->pPrev;
		pNewNode->pNext = pOldNode;
	}
	else
	{
		ASSERT(pOldNode == m_pNodeHead);
		pNewNode->pNext = pOldNode;
		m_pNodeHead = pNewNode;		
	}
	pOldNode->pPrev = pNewNode;
	return (POSITION) pNewNode;
}

POSITION CStringList::InsertAfter(POSITION position, LPCTSTR newElement)
{
	return InsertAfter(position, CString(newElement));
}

POSITION CStringList::InsertAfter(POSITION position, const CString& newElement)
{
	ATLASSERT(this);

	if (position == NULL)
		return AddTail(newElement); // insert after nothing -> tail of the list

	// Insert it before position
	CNode* pOldNode = (CNode*) position;
	CNode* pNewNode = new CNode;
	ASSERT( pNewNode != NULL );

	m_nCount ++;
	pNewNode->data = newElement;

	if (pOldNode->pNext != NULL)
	{
		pNewNode->pNext = pOldNode->pNext;
		pOldNode->pNext->pPrev = pNewNode;
		pNewNode->pPrev = pOldNode;
	}
	else
	{
		ASSERT(pOldNode == m_pNodeTail);
		pNewNode->pPrev = pOldNode;
		m_pNodeTail = pNewNode;
	}
	pOldNode->pNext = pNewNode;
	return (POSITION) pNewNode;
}

void CStringList::RemoveAt(POSITION position)
{
	ATLASSERT(this);
	CNode* pOldNode = (CNode*) position;

	// remove pOldNode from list
	if (pOldNode == m_pNodeHead)
	{
		m_pNodeHead = pOldNode->pNext;
	}
	else
	{
		pOldNode->pPrev->pNext = pOldNode->pNext;
	}
	if (pOldNode == m_pNodeTail)
	{
		m_pNodeTail = pOldNode->pPrev;
	}
	else
	{
		pOldNode->pNext->pPrev = pOldNode->pPrev;
	}
	FreeNode(pOldNode);
}

/////////////////////////////////////////////////////////////////////////////
// slow operations

POSITION CStringList::FindIndex(int nIndex) const
{
	ATLASSERT(this);

	if (nIndex >= m_nCount || nIndex < 0)
		return NULL;  // went too far

	CNode* pNode = m_pNodeHead;
	while (nIndex--)
	{
		pNode = pNode->pNext;
	}
	return (POSITION) pNode;
}

POSITION CStringList::Find(LPCTSTR searchValue, POSITION startAfter) const
{
	ATLASSERT(this);

	CNode* pNode = (CNode*) startAfter;
	if (pNode == NULL)
	{
		pNode = m_pNodeHead;  // start at head
	}
	else
	{
		//ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
		pNode = pNode->pNext;  // start after the one specified
	}

	for (; pNode != NULL; pNode = pNode->pNext)
		if (pNode->data == searchValue)
			return (POSITION) pNode;
	return NULL;
}

int CStringList::GetCount() const
	{ return m_nCount; }

BOOL CStringList::IsEmpty() const
	{ return m_nCount == 0; }

CString& CStringList::GetHead()
{
	ASSERT(m_pNodeHead != NULL);
	return m_pNodeHead->data; 
}

CString CStringList::GetHead() const
{
	ASSERT(m_pNodeHead != NULL);
	return m_pNodeHead->data; 
}

CString& CStringList::GetTail()
{
	ASSERT(m_pNodeTail != NULL);
	return m_pNodeTail->data; 
}

CString CStringList::GetTail() const
{
	ASSERT(m_pNodeTail != NULL);
	return m_pNodeTail->data; 
}

POSITION CStringList::GetHeadPosition() const
{
	return (POSITION) m_pNodeHead; 
}

POSITION CStringList::GetTailPosition() const
{
	return (POSITION) m_pNodeTail; 
}

CString& CStringList::GetNext(POSITION& rPosition) // return *Position++
{ 
	 CNode* pNode = (CNode*) rPosition;
	 rPosition = (POSITION) pNode->pNext;
	 return pNode->data; 
}

CString CStringList::GetNext(POSITION& rPosition) const // return *Position++
{
	CNode* pNode = (CNode*) rPosition;
	//ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
	rPosition = (POSITION) pNode->pNext;
	return pNode->data; 
}

CString& CStringList::GetPrev(POSITION& rPosition) // return *Position--
{
	CNode* pNode = (CNode*) rPosition;
	//ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
		rPosition = (POSITION) pNode->pPrev;
		return pNode->data; }
CString CStringList::GetPrev(POSITION& rPosition) const // return *Position--
{
	CNode* pNode = (CNode*) rPosition;
	//ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
	rPosition = (POSITION) pNode->pPrev;
	return pNode->data; 
}

CString& CStringList::GetAt(POSITION position)
{ 
	CNode* pNode = (CNode*) position;
	//ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
	return pNode->data; 
}

CString CStringList::GetAt(POSITION position) const
{ 
	CNode* pNode = (CNode*) position;
	//ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
	return pNode->data; 
}

void CStringList::SetAt(POSITION pos, LPCTSTR newElement)
{ 
	CNode* pNode = (CNode*) pos;
	////ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
	pNode->data = newElement; 
}

void CStringList::SetAt(POSITION pos, const CString& newElement)
{ 
	CNode* pNode = (CNode*) pos;
	//ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
	pNode->data = newElement; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -