|
lete pToBeDeleted;
pToBeDeleted = NULL;
}
}
else
{
cout << "链表为空!" << endl;
}
}
//创建一个链表,输入从头到尾结点的值,输入-1表示结束
void CreateList(ListNode *& pHead)
{
ListNode *pListNode = NULL;
ListNode *pCurLastNode = NULL;
bool isHead = true;
while (1)
{
if (isHead)
{
pHead = new ListNode();
cin >> pHead->m_nValue;
pHead->m_pNext = NULL;
isHead = false;
pCurLastNode = pHead;
}
else
{
pListNode = new ListNode();
cin >> pListNode->m_nValue;
if (pListNode->m_nValue == -1)
{
break;
}
pListNode->m_pNext = NULL;
pCurLastNode->m_pNext = pListNode;
pCurLastNode = pListNode;
}
}
}
//从头到尾打印链表
void PrintList(ListNode *pHead)
{
if (pHead != NULL)
{
ListNode *pCur = pHead;
while (pCur != NULL)
{
cout << pCur->m_nValue << " ";
pCur = pCur->m_pNext;
}
cout << endl;
}
else
{
cout << "链表为空!" << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
ListNode *pHead = NULL;
//创建链表
CreateList(pHead);
PrintList(pHead);
//删除头结点
DeleteNode(&pHead, pHead);
PrintList(pHead);
//删除第二个结点
if (pHead != NULL)
{
DeleteNode(&pHead, pHead->m_pNext);
PrintList(pHead);
}
//删除尾结点
ListNode *pCur = pHead;
if (pCur != NULL)
{
while (pCur->m_pNext != NULL)
{
pCur = pCur->m_pNext;
}
DeleteNode(&pHead, pCur);
PrintList(pHead);
}
system("pause");
return 0;
}
?
|