typedef struct SingList
{
int nData;
struct SingList *pNext;
}SingnNode,*pSingnNode;
typedef struct LoopList
{
int nData;
struct LoopList *pLeft,*pRight;
}LoopNode,*pLoopNode;
//单向还需修改哈,搜索的时候是返回前一个位置还是当前位置
void CSingnList::InestMyList(pSingnNode pHead,int nData,int nNum)
{
pSingnNode pTemp,pNewNode;
if ((pTemp = SerachMyList(pHead,nData)) == NULL)
{
strErrMsg.Format(_T("Can not find the node :[%d]!"),nData);
return;
}
if ((pNewNode = new SingnNode) == NULL)
{
strErrMsg.Format(_T("Can not new memory!"));
return;
}
pNewNode->nData = nData;
pNewNode->pNext = pTemp;//插在之前的位置
pTemp->pNext = pNewNode;
}
void CSingnList::DelMyList(pSingnNode pHead,int nData)
{
pSingnNode pTemp,pTemp_Next;
if ((pTemp = SerachMyList(pHead,nData)) == NULL)//这里应该返回要删除节点的前一个位置
{
strErrMsg.Format(_T("Can not find the node :[%d]!"),nData);
return;
}
pTemp_Next = pTemp->pNext;
pTemp->pNext = pTemp_Next->pNext;
delete pTemp_Next;
}
void CSingnList::PrintMyList(pSingnNode pHead)
{
int nOutData;
pSingnNode pTemp;
pTemp = pHead->pNext;
if (pTemp == NULL)
strErrMsg.Format(_T("That ia a empty LoopNode!"));
while(pTemp != NULL)
{
nOutData = pTemp->nData;
pTemp = pTemp->pNext;
}
}
pSingnNode CSingnList::SerachMyList(pSingnNode pHead,int nData)
{
pSingnNode pTemp;
pTemp = pHead->pNext;
while(pTemp != NULL)
{
if (nData == pTemp->nData)
{
return pTemp;
}
else
{
pTemp = pTemp->pNext;
}
}
return NULL;
}
pSingnNode CSingnList::CreateMyList(int nNum)
{
pSingnNode pHead,pNewNode,pTemp;
if ((pHead = new SingnNode) == NULL)
{
strErrMsg.Format(_T("Can not new memory!"));
return NULL;
}
pHead->nData = 0;
pHead->pNext = NULL;
pTemp = pHead;
for (int i = 0; i < nNum; i++)
{
if ((pNewNode = new SingnNode) == NULL)
{
strErrMsg.Format(_T("Can not new memory!"));
return NULL;
}
pNewNode->nData = i + 1;
pTemp->pNext = pNewNode;
pNewNode->pNext = NULL;
pTemp = pNewNode;
}
return pHead;
}