#include <iostream>
#include <stack>
using namespace std;
//链表的结构体
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
ListNode()
{
m_pNext = NULL;
}
};
//判断链表是否为空
bool isEmpty(ListNode* list)
{
return list->m_pNext == NULL;
}
//判断position是否是最后一个
bool isLast(ListNode* position, ListNode* list)
{
return position->m_pNext == NULL;
}
//在链表中找到某个元素
ListNode* Find(int value, ListNode* list)
{
ListNode* pNode = list->m_pNext;//指向第一个结点,list为头结点,不存放数据
while (pNode != NULL && pNode->m_nValue != value)
{
pNode = pNode->m_pNext;
}
return pNode;
}
//找到某一元素的前驱结点
ListNode* FindPrevious(int value, ListNode* list)
{
//指向第一个结点
ListNode* pNode = list;
while (pNode->m_pNext != NULL && pNode->m_pNext->m_nValue != value)
{
pNode = pNode->m_pNext;
}
return pNode;
}
//删除某个结点
void DeleteNode(int value, ListNode* list)
{
if (list == NULL)
{
return;
}
//要将删除的结点进行备份
ListNode* pDeleteNode = NULL;
ListNode* pNode = list;
while (pNode->m_pNext != NULL && pNode->m_pNext->m_nValue != value)
{
pNode = pNode->m_pNext;
}
if (pNode->m_pNext != NULL)
{
pDeleteNode = pNode->m_pNext;//待删除的结点
pNode->m_pNext = pDeleteNode->m_pNext;//待删除结点的下一个结点
//释放待删除的结点
delete pDeleteNode;
pDeleteNode = NULL;
}
return;
}
//将一个元素插入到pToBeInsertNode指示的结点之后,元素的值为value;
void InsertNode(int value, ListNode* list, ListNode* pToBeInsertNode)
{
if (list == NULL || pToBeInsertNode == NULL)
{
return;
}
ListNode* pNewNode = new ListNode();
if (pNewNode == NULL)
{
return;
}
pNewNode->m_nValue = value;
pNewNode->m_pNext = pToBeInsertNode->m_pNext;
pToBeInsertNode->m_pNext = pNewNode;
} //在末尾添加元素
void AddToTail( ListNode* list, int value)
{
//建立一个新节点
ListNode* pNewNode = new ListNode();
if (pNewNode == NULL)
{
return;
}
pNewNode->m_nValue = value;
pNewNode->m_pNext = NULL;
if (list == NULL)
{
list = pNewNode;
}
else
{
ListNode* tmpNode = list;
while (tmpNode->m_pNext != NULL)
{
tmpNode = tmpNode->m_pNext;
}
tmpNode->m_pNext = pNewNode;//最后一个的下一个结点指向新节点
}
}
//删除整个链表
void DeleteList(ListNode*list) {
if (list == NULL)
{
return;
}
ListNode*pNode = list->m_pNext;
ListNode*pTemp = NULL; list->m_pNext = NULL; //断开头结点
while (pNode != NULL)
{
pTemp = pNode->m_pNext;//需要保存其下一个节点
delete pNode;
pNode = pTemp; //移到下一个节点
}
} //打印链表
void printList(ListNode* list)
{
if (list == NULL)
{
return;
}
ListNode* pNode = list->m_pNext;
while (pNode != NULL)
{
cout << pNode->m_nValue << " ";
pNode = pNode->m_pNext;
}
cout << endl;
}
//逆序打印链表
void printListReversingly(ListNode* list)
{
stack<ListNode*> nodes;
ListNode* pNode = list->m_pNext;
while (pNode != NULL)
{
nodes.push(pNode);//压栈
pNode = pNode->m_pNext;
}
while (!nodes.empty())
{
ListNode* pTmp = nodes.top();//获取顶端元素
cout << pTmp->m_nValue << " ";
nodes.pop();//弹出
}
cout << endl;
}
//判断一个链表是否有环
bool LinkListLoop(ListNode* pHead)
{
ListNode* p = pHead;
ListNode* q = pHead;
while (p != NULL && q != NULL)
{
p = p->m_pNext;//p走一步;
q = q->m_pNext;
if (q->m_pNext != NULL)
q = q->m_pNext;//q走两步;
if (p != NULL && p == q)
{
return true;
}
}
return false;
}
//测试
int main() {
ListNode* list = new ListNode();
for (int i = ; i < ; i++)
{
AddToTail(list, i);
}
cout << "打印链表:" << endl;
printList(list); cout << "逆序打印链表:" << endl;
printListReversingly(list); cout << "在末尾添加元素:10" << endl;
AddToTail(list, );
printList(list); cout << "移除节点值为5的节点:" << endl;
DeleteNode(, list);
printList(list); cout << "找到节点值为6的节点的前驱节点:" << endl;
ListNode* pNode = FindPrevious(, list);
cout << pNode->m_nValue << endl; cout << "删除链表:" << endl;
DeleteList(list);
printList(list); cout << "链表是否为空:" << endl;
cout << isEmpty(list) << endl;
system("pause");
return ;
}