#include <iostream>
typedef int ElemType;
using namespace std;
typedef struct Node
{
ElemType Data;
struct Node *m_pNext;
}NodeList;
NodeList *InitNodeList();//创建头结点 并指向空
void CreateNodeList(NodeList *m_pHead);//创建一个链表
void PrintNodeList(NodeList *m_pHead);//打印链表
void ModNodeList(NodeList *m_pHead);//修改节点内容
void DelNodelist(NodeList *m_pHead);//删除第n个节点
void ClearNodeList(NodeList *m_pHead);//清空链表
void SortNodeList(NodeList *m_pHead);//链表排序
void SwapNodeList(NodeList *node1,NodeList *node2);//实现节点交换
NodeList *InitNodeList()
{
NodeList *m_pHead = (NodeList *)malloc(sizeof(NodeList));
if(NULL == m_pHead)
{
cout << "申请空间失败!\n";
return NULL;
}
m_pHead->m_pNext = NULL;
return m_pHead;
}
void CreateNodeList(NodeList *m_pHead)
{
//新节点 用于存储新放的数据
ElemType Elem; //用于存放数据
cout << "输入插入的数据,以-1结束:\n";
while(true)
{
NodeList *m_pNewNode = (NodeList *)malloc(sizeof(NodeList));
if(NULL == m_pNewNode)
{
cout << "申请空间失败!\n";
return;
}
cin >>Elem;
if(-1 == Elem)
{
break;
}
m_pNewNode->Data = Elem;//节点传入数据
m_pHead->m_pNext = m_pNewNode;//头结点的下个节点指向新节点
m_pNewNode->m_pNext = NULL;//新节点的下个节点指向空
m_pHead = m_pHead->m_pNext;//节点后移
}
}
void PrintNodeList(NodeList *m_pHead)
{
cout << "链表内容如下:\n";
if(NULL == m_pHead->m_pNext)
{
cout << "链表为空!\n";
return;
}
NodeList *m_pNewNode = (NodeList *)malloc(sizeof(NodeList));
if(NULL == m_pNewNode)
{
cout << "申请空间失败!\n";
return;
}
m_pNewNode = m_pHead;//指向头结点
while (m_pNewNode->m_pNext)
{
m_pNewNode = m_pNewNode->m_pNext;
cout << m_pNewNode->Data << " ";
}
cout << endl;
}
void ModNodeList(NodeList *m_pHead)
{
if(NULL == m_pHead->m_pNext)
{
cout << "链表为空!\n";
return;
}
cout << "输入修改的值:\n";
ElemType elem,data;
cin >> elem;
cout << "请输入修改后的值:\n";
cin >> data;
while (m_pHead->m_pNext)
{
if(elem == m_pHead->m_pNext->Data)
{
m_pHead->m_pNext->Data = data;
return;
}
m_pHead = m_pHead->m_pNext;//没找到则后移一位
}
cout<< "链表中不存在该节点!\n";
}
void DelNodelist(NodeList *m_pHead)
{
NodeList *m_pNode = m_pHead;//用作移动
NodeList *m_pDelNode;
if(NULL == m_pNode)
{
cout << "链表为空!\n";
return;
}
int n;//删除位置
int i = 1;
cout << "请删除的位置:\n";
while (true)
{
cin >> n;
if(n < 1)
{
cout << "输入的位置错误!请重新输入!\n";
}
else
{
break;
}
}
while (i<n)
{
m_pNode = m_pNode->m_pNext;//移动到下个节点
if(!(m_pNode->m_pNext))
{
cout << "位置错误!\n";
return;
}
i++;
}
m_pDelNode = m_pNode->m_pNext;//删除的位置为下一个因为开始m_pNode指向头结点
m_pNode->m_pNext = m_pDelNode->m_pNext;
free(m_pDelNode);
m_pDelNode = NULL;
}
void ClearNodeList(NodeList *m_pHead)
{
NodeList *m_pNode = m_pHead;
NodeList *m_pDelNode = NULL;//用来标记删除节点
if(m_pNode->m_pNext == NULL)
{
cout << "此链表为空!\n";
return;
}
m_pNode = m_pHead->m_pNext;
while (m_pNode)
{
m_pDelNode = m_pNode;//存储节点 删除
m_pNode = m_pNode->m_pNext;
free(m_pDelNode);
m_pDelNode = NULL;
}
//删除完后 头结点指向空
m_pHead->m_pNext = NULL;
return;
}
void SwapNodeList(NodeList *node1,NodeList *node2)
{
NodeList tmp = *node1;
tmp.m_pNext = node2->m_pNext;
node2->m_pNext = node1->m_pNext;
*node1 = *node2;
*node2 = tmp;
}
void SortNodeList(NodeList *m_pHead)
{
NodeList *m_pNode = m_pHead->m_pNext;
NodeList *m_pMinNode = NULL;
if(NULL == m_pNode)
{
cout << "该链表无需排序!\n";
return;
}
while (m_pNode)
{
m_pMinNode = m_pNode; //第一个节点
NodeList *m_pNodeList = m_pNode->m_pNext;//第二个节点
if(!m_pNodeList)
{
return;
}
while (m_pNodeList)
{
if(m_pNodeList->Data < m_pMinNode->Data)
{
m_pMinNode = m_pNodeList;//遍历存放最小节点
}
m_pNodeList = m_pNodeList->m_pNext;//指向下一个
}
//完成数据交换
SwapNodeList(m_pNode,m_pMinNode);
m_pNode = m_pNode->m_pNext;
}
}
int main()
{
NodeList *m_pHead = NULL;
m_pHead = InitNodeList();
CreateNodeList(m_pHead);
PrintNodeList(m_pHead);
// ModNodeList(m_pHead);
// cout << "修改后的内容为:\n";
// PrintNodeList(m_pHead);
// DelNodelist(m_pHead);
// cout << "删除后的内容为:\n";
// PrintNodeList(m_pHead);
// ClearNodeList(m_pHead);
// cout << "清空后内容为:\n";
// PrintNodeList(m_pHead);
SortNodeList(m_pHead);
cout << "排序后内容为:\n";
PrintNodeList(m_pHead);
return 0;
}
链表的实现
最新推荐文章于 2024-08-29 11:14:05 发布