关于链表的构建及基本操作
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
};
ListNode* CreateListNode(int value);
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext);
void PrintListNode(ListNode* pNode);
void PrintList(ListNode* pHead);
void DestroyList(ListNode* pHead);
void AddToTail(ListNode** pHead, int value);
void RemoveNode(ListNode** pHead, int value);
ListNode* CreateListNode(int value)
{
ListNode* pNode = new ListNode();
pNode->m_nValue = value;
pNode->m_pNext = nullptr;
return pNode;
}
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
if (pCurrent == nullptr)
{
printf("Error to connect two nodes.\n");
exit(1);
}
pCurrent->m_pNext = pNext;
}
void PrintListNode(ListNode* pNode)
{
if (pNode == nullptr)
{
printf("The node is nullptr\n");
}
else
{
printf("The key in node is %d.\n", pNode->m_nValue);
}
}
void PrintList(ListNode* pHead)
{
printf("PrintList starts.\n");
ListNode* pNode = pHead;
while (pNode != nullptr)
{
printf("%d\t", pNode->m_nValue);
pNode = pNode->m_pNext;
}
printf("\nPrintList ends.\n");
}
void DestroyList(ListNode* pHead)
{
ListNode* pNode = pHead;
while (pNode != nullptr)
{
pHead = pHead->m_pNext;
delete pNode;
pNode = pHead;
}
}
void AddToTail(ListNode** pHead, int value)
{
ListNode* pNew = new ListNode();
pNew->m_nValue = value;
pNew->m_pNext = nullptr;
if (*pHead == nullptr)
{
*pHead = pNew;
}
else
{
ListNode* pNode = *pHead;
while (pNode->m_pNext != nullptr)
pNode = pNode->m_pNext;
pNode->m_pNext = pNew;
}
}
void RemoveNode(ListNode** pHead, int value)
{
if (pHead == nullptr || *pHead == nullptr)
return;
ListNode* pToBeDeleted = nullptr;
if ((*pHead)->m_nValue == value)
{
pToBeDeleted = *pHead;
*pHead = (*pHead)->m_pNext;
}
else
{
ListNode* pNode = *pHead;
while (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue != value)
pNode = pNode->m_pNext;
if (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue == value)
{
pToBeDeleted = pNode->m_pNext;
pNode->m_pNext = pNode->m_pNext->m_pNext;
}
}
if (pToBeDeleted != nullptr)
{
delete pToBeDeleted;
pToBeDeleted = nullptr;
}
}
加入for循环之后,更易创建链表
#include <iostream>
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
};
ListNode* CreateListNode(int value);
void ConnectListNodes(ListNode* pCurrent, int num);
void PrintListNode(ListNode* pNode);
void PrintList(ListNode* pHead);
void DestroyList(ListNode* pHead);
/*void AddToTail(ListNode** pHead, int value);
void RemoveNode(ListNode** pHead, int value);*/
using namespace std;
int main()
{
int n, valueOfHead;
cin >> n;
cin >> valueOfHead;
ListNode* Head = CreateListNode(valueOfHead);
ConnectListNodes(Head, n);
PrintList(Head);
DestroyList(Head);
return 0;
}
ListNode* CreateListNode(int value)
{
ListNode* pHead = new ListNode();
pHead->m_nValue = value;
pHead->m_pNext = nullptr;
return pHead;
}
void ConnectListNodes(ListNode* pHead, int n)
{
if (pHead == nullptr)
{
printf("Error to connect two nodes.\n");
exit(1);
}
ListNode* pNode = pHead;
ListNode* s;
for (int i = 1; i < n; ++i)
{
s = new ListNode();
cin >> s->m_nValue;
s->m_pNext = nullptr;
pNode->m_pNext = s;
pNode = s;
}
}
void PrintListNode(ListNode* pNode)
{
if (pNode == nullptr)
{
printf("The node is nullptr\n");
}
else
{
printf("The key in node is %d.\n", pNode->m_nValue);
}
}
void PrintList(ListNode* pHead)
{
printf("PrintList starts.\n");
ListNode* pNode = pHead;
while (pNode != nullptr)
{
printf("%d\t", pNode->m_nValue);
pNode = pNode->m_pNext;
}
printf("\nPrintList ends.\n");
}
void DestroyList(ListNode* pHead)
{
ListNode* pNode = pHead;
while (pNode != nullptr)
{
pHead = pHead->m_pNext;
delete pNode;
pNode = pHead;
}
}
/*void AddToTail(ListNode** pHead, int value)
{
ListNode* pNew = new ListNode();
pNew->m_nValue = value;
pNew->m_pNext = nullptr;
if (*pHead == nullptr)
{
*pHead = pNew;
}
else
{
ListNode* pNode = *pHead;
while (pNode->m_pNext != nullptr)
pNode = pNode->m_pNext;
pNode->m_pNext = pNew;
}
}
void RemoveNode(ListNode** pHead, int value)
{
if (pHead == nullptr || *pHead == nullptr)
return;
ListNode* pToBeDeleted = nullptr;
if ((*pHead)->m_nValue == value)
{
pToBeDeleted = *pHead;
*pHead = (*pHead)->m_pNext;
}
else
{
ListNode* pNode = *pHead;
while (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue != value)
pNode = pNode->m_pNext;
if (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue == value)
{
pToBeDeleted = pNode->m_pNext;
pNode->m_pNext = pNode->m_pNext->m_pNext;
}
}
if (pToBeDeleted != nullptr)
{
delete pToBeDeleted;
pToBeDeleted = nullptr;
}
}*/
关于单链表可参考博文
单链表的生成方法(C++)及单链表遍历一次中值查找