//单链表
#include<iostream>
#include<string>
using namespace std;
typedef struct Lnode {
int data;
struct Lnode *next;
} Lnode, *LinkList; // LinkList为指向结构体Lnode的指针类型
// 初始化链表
int InitList_L(LinkList &L) {
L = new Lnode;
L->next = NULL;
return 1;
}
// 判断链表是否为空
int IsEmpty_L(LinkList &L) {
if (L->next == nullptr) {
cout << "链表为空" << endl;
return 1;
} else {
cout << "链表不为空" << endl;
return 0;
}
}
// 清空链表
void Destroy_L(LinkList &L) {
LinkList p;
LinkList q;
p = L->next;
while (p != nullptr) {
q = p->next;
delete p;
p = q;
}
L->next = nullptr;
cout << "初始化链表成功" << endl;
}
// 获取链表长度
int GetLength_L(LinkList &L) {
int count = 0;
LinkList p;
p = L->next; // 此时p指向头结点
if (p == nullptr) {
return 0;
}
while (p != nullptr) {
p = p->next; // 第一次循环p由头结点变成指向首元节点
count++;
}
return count;
}
// 获取某个元素的数据域
int GetElem_L(LinkList &L, int i) {
LinkList p;
p = L->next;
for (int j = 0; j < i; j++) {
if (p == nullptr) {
cout << "链表为空" << endl;
return -1;
}
p = p->next;
}
if (p == nullptr) {
cout << "链表为空" << endl;
return -1;
}
int x = p->data;
return x;
}
// 在线性表L中查找值为e的数据元素
LinkList LocateElem_L(LinkList &L, int e) {
LinkList p;
p = L->next;
while (p != nullptr && p->data != e) {
p = p->next;
}
return p;
}
// 按值查找--根据指定数据获取该数据的位置序号j
int LocateElem2_L(LinkList &L, int e) {
int j = 0;
LinkList p;
p = L->next;
while (p != nullptr && p->data != e) {
p = p->next;
j++;
}
if (p != nullptr) {
return j+1;
} else {
return 0;
}
}
// 在L中第i个元素之前插入数据元素e
int ListInsert_L(LinkList &L, int i, int e) {
LinkList p;
p = L; // 从头结点开始
int j = 0;
while (p != nullptr && j < i - 1) {
p = p->next;
j++; // 找到指向i-1元素的指针p
}
if (p == nullptr || j > i - 1) {
return -1;
}
Lnode *s = new Lnode; // 生成新结点
s->next = p->next; // p->next是第i个结点的地址
p->next = s;
s->data = e;
return 1;
}
//删除某位置元素
int Delete_L(LinkList &L, int i) {
LinkList p;
p = L; // 从头结点开始
int j = 0;
// 找到第i-1个节点
while (p != nullptr && j < i - 1) {
p = p->next;
j++;
}
// 如果p为空或p的下一个节点为空,说明位置i不合法
if (p == nullptr || p->next == nullptr) {
return -1;
}
LinkList q = p->next; // 临时保存被删除结点的地址以备删除
p->next = q->next;
int e = q->data;
delete q;
return 1;
}
void CreateList_L(LinkList &L,int n)//头插法创建链表
{
//n指创建n个结点
L = new Lnode;
L->next = NULL;//建立一个带头结点的链表
for(int i = n;i>0;i--)
{
LinkList p = new Lnode;
cout<<"请输入数据域";
cin>>p->data;
p->next = L->next;
L->next = p;
}
}
void CreateList2_L(LinkList &L,int n)
{
//n指创建n个结点
L = new Lnode;
L->next = NULL;//建立一个带头结点的空链表
LinkList r = L;//尾指针r指向头结点
for(int i =0;i<n;i++)
{
LinkList p = new Lnode;
cout<<"请输入数据域"<<endl;
cin>>p->data;
r->next = p;
r = p;
}
}
// 打印链表
void PrintList_L(LinkList &L) {
LinkList p = L->next;
while (p != nullptr) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main() {
LinkList L;
InitList_L(L);
ListInsert_L(L,1,5);
ListInsert_L(L,2,6);
ListInsert_L(L,3,7);
ListInsert_L(L,4,8);
ListInsert_L(L,5,9);
ListInsert_L(L,6,10);
ListInsert_L(L,7,11);
// 输出线性表
cout << "线性表内容: ";
PrintList_L(L);
cout<<"-------------------------"<<endl;
cout<<"在线性表L中查找值为e的数据元素"<<" "<<LocateElem_L(L,8)<<endl;
cout<<"-------------------------"<<endl;
cout<<"按值查找--根据指定数据获取该数据的位置序号j"<<" "<<LocateElem2_L(L,8)<<endl;
cout<<"-------------------------"<<endl;
cout<<"删除某位置元素 "<<Delete_L(L,5)<<endl;
PrintList_L(L);
cout<<"-------------------------"<<endl;
IsEmpty_L(L);
cout<<"-------------------------"<<endl;
CreateList_L(L,2);
PrintList_L(L);
cout<<"-------------------------"<<endl;
cout<<"在线性表L中查找值为e的数据元素"<<" "<<LocateElem_L(L,77)<<endl;
cout<<"-------------------------"<<endl;
cout<<"按值查找--根据指定数据获取该数据的位置序号j"<<" "<<LocateElem2_L(L,77)<<endl;
cout<<"-------------------------"<<endl;
cout<<"删除某位置元素 "<<Delete_L(L,5)<<endl;
PrintList_L(L);
cout<<"-------------------------"<<endl;
IsEmpty_L(L);
cout<<"-------------------------"<<endl;
cout<<"头插法";
CreateList_L(L,3);
PrintList_L(L);
cout<<"-------------------------"<<endl;
cout<<"尾插法";
CreateList2_L(L,3);
PrintList_L(L);
return 0;
}