1、链式存储的操作

LinkList InitList(LinkList &L);
LinkList List_HeadInsert(LinkList &L, int n);
LinkList List_TailInsert(LinkList &L, int n);
void PrintList(LinkList L);
bool Empty(LinkList L);
ElemType GetElem(LinkList L, int i);
Status LocateElem(LinkList L, int e);
bool ListInsert(LinkList &L, int i, int e);
bool ListDelete(LinkList &L, int i, int &e);
void DestoryList(LinkList &L);
Status GetLength(LinkList L);

数据结构

typedef struct LNode{
ElemType data;
struct LNode* next;
}*LinkList;

2、函数实现

2.1 链表初始化

//初始化链表
LinkList InitList(LinkList &L){
L = new LNode;
L->next = NULL;
}

2.2 头插法

//头插法
LinkList List_HeadInsert(LinkList &L, int n){
LinkList r = L->next;
for (int i = 1; i < n; i++){
LinkList p = InitList(p);
p->data = i*2;
L->next = p;
p->next = r;
r = p;
}
return L;
}

2.3 尾插法

//尾插法
LinkList List_TailInsert(LinkList &L, int n){
LinkList r = L;
for (int i = 1; i < n; i++){
LinkList p = InitList(p);
p->data = i*2;
r->next = p;
r = p;
}
return L;
}

2.4 打印链表

//打印单链表
void PrintList(LinkList L){
LinkList t = L->next;
while(t){
cout << t->data << " ";
t = t->next;
}
cout << endl;
}

2.5 判断为空

//判断但链表是否为空
bool Empty(LinkList L){
return L==NULL;
}

2.6 求第i个元素

//获取第i个位置上的数据
ElemType GetElem(LinkList L, int i){
LinkList t = L;
while (i--){
t = t->next;
}
return t->data;
}

2.7 获取数据e的索引

//获取e在第几位
Status LocateElem(LinkList L, int e){
LinkList t = L;
int i = 0;
while(t){
if (t->data == e){
return i;
}
i++;
t = t->next;
}
return -1;
}

2.8 在第i个位置插入数据e

//在第i位置插入e
bool ListInsert(LinkList &L, int i, int e){
//插入后面
LinkList p = L;
while(i--){
p = p->next;
}

/*
//插入前面
LinkList p = L;
while(--i){
p = p->next;
}
*/

LinkList t = InitList(t);
t->data = e;
t->next = p->next;
p->next = t;
return true;
}

2.9 删除第i个位置上的数据

//删除第i个位置数据
bool ListDelete(LinkList &L, int i, int &e){
LinkList p = L;
while(--i){
p = p->next;
}
LinkList t = p->next;
e = t->data;
p->next = p->next->next;
delete(t);

return true;
}

2.11 获取长度

Status GetLength(LinkList L){
int len = 0;
LinkList t = L;
while(t){
len++;
t = t->next;
}
return len;
}

2.11 销毁链表

//销毁链表
void DestoryList(LinkList &L){
LinkList p;
while(L){
p = L;
L = L->next;
delete p;
}

if(L == nullptr){
cout << "successfully" << endl;
}else{
cout << "fail" << endl;
}
}

3、 完整代码

#include<iostream>
using namespace std;

typedef int ElemType;
typedef int Status;

typedef struct LNode{
ElemType data;
struct LNode* next;
}*LinkList;

LinkList InitList(LinkList &L);
LinkList List_HeadInsert(LinkList &L, int n);
LinkList List_TailInsert(LinkList &L, int n);
void PrintList(LinkList L);
bool Empty(LinkList L);
ElemType GetElem(LinkList L, int i);
Status LocateElem(LinkList L, int e);
bool ListInsert(LinkList &L, int i, int e);
bool ListDelete(LinkList &L, int i, int &e);
void DestoryList(LinkList &L);
Status GetLength(LinkList L);
int main(){
LinkList L = InitList(L);
//初始化链表
cout << "Initiate List successfully" << endl;
//头插法
//L = List_HeadInsert(L,8);
//PrintList(L);

//尾插法
L = List_TailInsert(L,8);
PrintList(L);

cout << "I want to know whether L is NULL" << endl;
if(Empty(L)){
cout << "L is empty";
}else{
cout << "L is not empty";
}

cout << "I want to get the third number" << endl;
cout << "The third number is :" <<GetElem(L,3) << endl;

cout << "I want know where is 8" << endl;
cout << "Location of 8 is :" << LocateElem(L,8) << endl;

cout << "Inert 9 in index 4" << endl;
if (ListInsert(L,4,9)){
PrintList(L);
}

cout << "Delete index 6 " << endl;
int e;
if (ListDelete(L,6,e)){
PrintList(L);
cout << "e:" << e << endl;
}

cout << "Get len of L" << endl;
cout << GetLength(L) << endl;

cout << "Finally, I will destory L" << endl;
DestoryList(L);

return 0;
}

//初始化链表
LinkList InitList(LinkList &L){
L = new LNode;
L->next = NULL;
}

//头插法
LinkList List_HeadInsert(LinkList &L, int n){
LinkList r = L->next;
for (int i = 1; i < n; i++){
LinkList p = InitList(p);
p->data = i*2;
L->next = p;
p->next = r;
r = p;
}
return L;
}

//尾插法
LinkList List_TailInsert(LinkList &L, int n){
LinkList r = L;
for (int i = 1; i < n; i++){
LinkList p = InitList(p);
p->data = i*2;
r->next = p;
r = p;
}
return L;
}

//打印单链表
void PrintList(LinkList L){
LinkList t = L->next;
while(t){
cout << t->data << " ";
t = t->next;
}
cout << endl;
}

//判断但链表是否为空
bool Empty(LinkList L){
return L==NULL;
}

//获取第i个位置上的数据
ElemType GetElem(LinkList L, int i){
LinkList t = L;
while (i--){
t = t->next;
}
return t->data;
}

//获取e在第几位
Status LocateElem(LinkList L, int e){
LinkList t = L;
int i = 0;
while(t){
if (t->data == e){
return i;
}
i++;
t = t->next;
}
return -1;
}

//在第i位置插入e
bool ListInsert(LinkList &L, int i, int e){
//插入后面
LinkList p = L;
while(i--){
p = p->next;
}

/*
//插入前面
LinkList p = L;
while(--i){
p = p->next;
}
*/

LinkList t = InitList(t);
t->data = e;
t->next = p->next;
p->next = t;
return true;
}

//删除第i个位置数据
bool ListDelete(LinkList &L, int i, int &e){
LinkList p = L;
while(--i){
p = p->next;
}
LinkList t = p->next;
e = t->data;
p->next = p->next->next;
delete(t);

return true;
}

//销毁链表
void DestoryList(LinkList &L){
LinkList p;
while(L){
p = L;
L = L->next;
delete p;
}

if(L == nullptr){
cout << "successfully" << endl;
}else{
cout << "fail" << endl;
}
}

Status GetLength(LinkList L){
int len = 0;
LinkList t = L;
while(t){
len++;
t = t->next;
}
return len;
}