一、废话不多说,直接看代码
// 创建、插入、删除、获取长度、获取端点等约十种基本操作
#include <iostream>
using namespace std;
// 定语单链表结构体
typedef struct LNode
{
int data; // 结点的数据域
LNode *next; // 结点的指针域
}LNode;
// 生成一结点
LNode *initList(void)
{
LNode *p = new LNode;
if(!p)
{
cout << "动态分配内存空间出错\n";
return NULL;
}
p->data = 0;
p->next = NULL;
return p;
}
// 头插法插入一个节点
void head_insert(LNode *head, LNode *p)
{
p->next = head->next;
head->next = p;
}
// 头插法创建一个链表
bool creatList(LNode *head, int len)
{
// 判断表是否存在
if(!head)
{
cout << "创建链表失败\n";
return false;
}
for(int i = 0; i < len; i++)
{
LNode *p = initList();
if(!p)
{
cout << "创建链表失败\n";
return false;
}
cout << "请输入结点数据:";
cin >> p->data;
head_insert(head, p);
}
return true;
}
// 计算链表的长度
int listLength(LNode *head)
{
// 判断表是否存在
if(!head)
{
return -1;
}
int i = 0;
for(LNode *p = head->next; p; p = p->next, i++);
return i;
}
// 判断是否为空表
int isEmpty(LNode *head)
{
// 判断表是否存在
if(!head)
{
return -1;
}
return !head->next;
}
// 头删法,将链表清空
bool clearList(LNode *head)
{
// 判断表是否存在
if(!head)
{
return false;
}
LNode *p;
while(head->next)
{
p = head->next; // 弹出首元节点
head->next = p->next; // head->next指向下一个节点
delete p;
}
return true;
}
// 头删法,销毁链表
bool destroyList(LNode *head)
{
if(clearList(head))
{
delete head;
return true;
}
return false;
}
// 查找某个节点, 成功返回指向该节点的指针
LNode *findNode(LNode *head, int data)
{
// 判断表是否存在
if(!head)
{
return NULL;
}
LNode *p;
for(p = head->next; p; p = p->next)
{
if(p->data == data)
{
return p;
}
}
return NULL;
}
// 修改某个节点数据
bool modifyNode(LNode *head, int old_data, int new_data)
{
LNode *p = findNode(head, old_data);
// 判断节点是否存在
if(!p)
{
return false;
}
p->data = new_data;
return true;
}
// 遍历链表
bool traverseList(LNode *head)
{
// 判断表是否存在
if(!head)
{
return false;
}
for(LNode *p = head->next; p; p = p->next)
{
cout << p->data << " ";
}
cout << endl;
return true;
}
// 删除某个节点
int delNode(LNode *head, int data)
{
// 判断表是否存在
if(!head)
{
return -1;
}
LNode *p, *tmp;
for(p = head; p->next; p = p->next)
{
if(p->next->data == data)
{
tmp = p->next; // 临时保存要被删除的节点
p->next = tmp->next; // 修改指针
delete tmp;
return 1;
}
}
return 0;
}
int main()
{
cout << "下面开始模拟单链表的操作\n";
int i, len, data;
LNode *head, *p;
head = initList();
if(!head)
{
cout << "初始化头结点失败\n";
return -1;
}
while(1)
{
cout << "请选择你的操作,1.创建单链表 2.插入新节点 3.删除节点 ";
cout << "4.遍历链表 5.查找节点 6.链表长度 7.修改节点 8.清空链表 9.销毁链表 10.退出\n";
cin >> i;
switch(i)
{
case 1:
if(!head)
{
head = initList();
}
cout << "请输入链表的长度:";
cin >> len;
if(creatList(head, len))
{
cout << "创建成功\n";
}
else
{
cout << "创建失败\n";
}
break;
case 2:
cout << "请输入要插入的数据:";
p = initList();
if(!p)
{
cout << "分配节点空间失败\n";
}
else
{
cin >> p->data;
head_insert(head, p);
cout << "插入成功\n";
}
break;
case 3:
cout << "请输入要删除的数据:";
cin >> data;
if(delNode(head, data) < 0)
{
cout << "删除失败\n";
}
else
{
cout << "删除成功\n";
}
break;
case 4:
if(traverseList(head))
{
cout << "遍历成功\n";
}
else
{
cout << "遍历失败\n";
}
break;
case 5:
cout << "请输入要查找的数据:";
cin >> data;
if(findNode(head, data))
{
cout << "链表中存在该元素\n";
}
else
{
cout << "查找失败\n";
}
break;
case 6:
if(isEmpty(head) == -1)
{
cout << "链表不存在\n";
}
len = listLength(head);
if(len != -1)
{
cout << "链表长度:" << len << endl;
}
break;
case 7:
cout << "请输入要修改的节点的原数据和新数据:";
cin >> data >> len;
if(modifyNode(head, data, len))
{
cout << "修改成功\n";
}
else
{
cout << "节点不存在或者链表为空\n";
}
break;
case 8:
if(clearList(head))
{
cout << "清空成功\n";
}
else
{
cout << "链表不存在\n";
}
break;
case 9:
if(destroyList(head))
{
head = NULL;
cout << "销毁成功\n";
}
else
{
cout << "链表不存在\n";
}
break;
}
if(i == 10)
{
cout << "你已退出!\n";
break;
}
}
return 0;
}
二、简单分析
如果你学过数据结构,不是很理解线性表,看这个会对你有很大的帮助,代码里的注释到位,易错点指出,认真看基本能看懂。难点就是如何对一个线性表进行增删查改。