视频地址:https://www.bilibili.com/video/BV1nJ411V7bd
#include<iostream>
using namespace std;
//链表节点数据域
class ElemType
{
public:
int a;
};
//链表类
class Lnode
{
public:
ElemType data; //数据域
Lnode* next; //指针域
};
typedef Lnode* LinkList;
//链表初始化
bool initList(LinkList& L)
{
L = new Lnode;
if (L == NULL)
return false;
L->next = NULL;
Lnode* p;
return true;
}
//创建单向链表--头插法
//插入的新数据始终位于链表头部
//时间复杂度:O(n) 空间复杂度:O(1)
void createListHead(LinkList& L, int n)
{
for (int i = 0; i < n; i++)
{
Lnode* p = new Lnode;
p->next = L->next;
p->data.a = i;
L->next = p;
}
}
//创建单向链表--尾插法
//插入的新数据始终位于链表尾部
//时间复杂度:O(n) 空间复杂度:O(1)
void createListTail(LinkList& L, int n)
{
Lnode* p = L;
for (int i = 0; i < n; i++)
{
Lnode* q = new Lnode;
q->next = p->next;
q->data.a = i;
p->next = q;
p = p->next;//如果注释掉此行,即为头插法
}
}
//打印链表
void printList(LinkList& L)
{
Lnode* p = L->next;
int j = 0;
while (p)
{
cout << "第" << j << "位数据为:" << p->data.a << endl;
j++;
p = p->next;
}
}
//计算链表长度
int lenList(const LinkList& L)
{
int len = 0;
Lnode* p = L->next;
while (p) {
len++;
p = p->next;
}
return len;
}
//取第i个元素的值
bool getElem(const LinkList& L, int i, ElemType &e)
{
Lnode* p = L->next;
int j = 0;
while (p && j < i)
{
p = p->next;
++j;
}
if (!p || j > i) return false;
e= p->data;
return true;
}
//按值查找
int locateElem(const LinkList& L, ElemType& e)
{
Lnode* p;
int j = 0;
p = L->next;
while (p && p->data.a != e.a)
{
p = p->next;
j++;
}
if (!p) return -1;
return j;
}
//在第i个元素前插入节点
bool insertList(LinkList& L,int i)
{
Lnode* p = L;
int j = 0;
while (p && j < i )
{
p = p->next;
j++;
}
if (!p || j > i) return false;
Lnode* q = new Lnode;
cout << "请输入新节点的数据:" << endl;
cin >> q->data.a;
q->next = p->next;
p->next = q;
return true;
}
//删除第i个元素
bool deleteList(LinkList& L, int i)
{
Lnode* p = L;
int j = 0;
while (p->next && j < i)
{
p = p->next;
j++;
}
if (!(p->next) || j > i) return false;
Lnode* q = p->next;
p->next = q->next;
delete q ;
return true;
}
//清空链表:头节点和头指针还在
bool clearList(LinkList& L)
{
Lnode* p = L->next;
Lnode* q = NULL;
while (p)
{
q = p->next;
delete p;
p = q;
}
L->next = NULL;
return true;
}
//判断链表是否为空
bool listEmpty(LinkList& L)
{
if (L->next == NULL) return true;
return false;
}
//销毁链表
bool DestroyList(LinkList& L)
{
//判断链表是否为空
if (listEmpty(L))
{
cerr << "empty List!" << endl;
return false;
}
while (L != NULL)//链表还未到达尾端
{
Lnode* p ;
p = L;//将头指针指向下一个结点
L = L->next;
delete p;
}
return true;
}
int main()
{
LinkList L;
initList(L); //初始化
createListTail(L, 8); //生成测试单链表
printList(L); //打印链表
//测试销毁链表
//if (DestroyList(L))
// cout << "链表已销毁" << endl;
//计算链表长度
int len = lenList(L);
cout << "链表长度为:" << len << endl;
//获取链表中第i个元素的值(i从0开始)
ElemType e;
if(getElem(L, 3, e))
cout <<"链表第3个元素值为:" <<e.a << endl;
else cout << "链表不存在第3个元素!" << endl;
//查找值为e的数据元素的序号
if (locateElem(L, e) == -1)
cout << "链表中不存在值为" << e.a << "的数据元素!" << endl;
else
cout << "链表中值为" << e.a
<< "的数据元素在第" << locateElem(L, e) << "位!" << endl;
//在第i个元素前插入节点
if (insertList(L, 3))
{
cout << "插入成功!" << endl;
printList(L); //打印链表
}
else
{
cout << "插入失败!" << endl;
printList(L); //打印链表
}
//删除第i个节点
if (deleteList(L, 5))
{
cout << "删除成功!" << endl;
printList(L); //打印链表
}
else
{
cout << "删除失败!" << endl;
printList(L); //打印链表
}
//清空链表
if (clearList(L))
cout << "链表已清空!" << endl;
//判断链表是否为空
if (listEmpty(L)) cout << "链表为空!" << endl;
else cout << "链表不为空!" << endl;
system("pause");
return 0;
}