由于上次小编写的顺序表变量名称复杂,容易造成理解偏差,所以此次决定用接地气的方式来完成链表功能的实现。不说了,直接上代码!!!
#include <iostream>
#include<cstdlib>
#include<iomanip>
#define OK 1
#define ERROR 0
using namespace std;
//定义头结点
struct Lnode
{
int data;
Lnode *next;
};
//初始化链表
void InitList_L(Lnode *&L)
{
L=(Lnode *)malloc(sizeof(Lnode));
L->next=NULL;
}
//1.清空线性表
int ClearList(Lnode *&head)
{
Lnode *p,*q;
p=head->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
head->next=NULL;
return OK;
}
//2.判断链表是否为空
int ListEmpty(Lnode *&head)
{
if(head->next==NULL)
return 0;
else
return 1;
}
//3.求链表的长度
int ListLength(Lnode *&L)
{
Lnode *p;
p=L;
int count=0;
while(p->next!=NULL)
{
count++;
p=p->next;
}
return count;
}
//4.获取线性表指定位置的值
int GetElem(Lnode *&head,int i)
{
Lnode *p=head;
for(int j=0;j<=i-1;j++)
{
p=p->next;
}
if(p)
cout<<p->data;
}
//5.求前驱
int GetPre(Lnode *&head,int i)
{
Lnode *p=head;
if(i==1)
return ERROR;
else
{
for(int j=0;j<=i-2;j++)
p=p->next;
if(p)
cout<<p->data;
else
return ERROR;
}
}
//6.求后继
int GetNext(Lnode *&head,int i)
{
Lnode *p=head;
for(int j=0;j<=i-1;j++)
{
p=p->next;
}
if(p)
cout<<p->next->data;
}
//7.在链表的指定位置上插入元素
bool ListInsert_L(Lnode *&head,int i,int e)
{
Lnode *s;
Lnode *p=head;
int j=0;
while(p&&j<i-1)
{
p=p->next;
j++;
}
if(!p||j>i-1) return ERROR;
s=(Lnode *)malloc(sizeof(Lnode));
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
//8.在链表的指定位置上删除元素
bool ListDelete_L(Lnode *&head,int i,int e)
{
Lnode *p=head;
Lnode *r;
int j=0;
while(p->next&&j<i-1){
p=p->next;
j++;
}
if(!(p->next)||j>i-1) return ERROR; //删除位置不合理
r=p->next; //临时保存被删结点的地址以被释放
p->next=r->next;
e=r->data;free(r);
return OK;
}
//9.显示链表
void print(Lnode *head)
{
Lnode *p;
p=head->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
void show_help()
{
cout<<"1----清空线性表"<<endl;
cout<<"2----判断线性表是否为空"<<endl;
cout<<"3----求线性表长度"<<endl;
cout<<"4----获取线性表指定位置元素"<<endl;
cout<<"5----求前驱"<<endl;
cout<<"6----求后继"<<endl;
cout<<"7----在线性表指定位置插入元素"<<endl;
cout<<"8----删除线性表指定位置元素"<<endl;
cout<<"9----显示线性表"<<endl;
cout<<" 退出,输出一个负数!"<<endl;
}
int main()
{
int operate_code;
show_help();
Lnode *head;
Lnode *L;
InitList_L(head);
while(1)
{
cout<<"请输入操作代码:";
cin>>operate_code;
if(operate_code==1)
{
ClearList(head);
}
else if(operate_code==2)
{
if(ListEmpty(head)==0)
cout<<"The list is empty."<<endl;
else
cout<<"The list is not empty."<<endl;
}
else if(operate_code==3)
{
cout<<"The length of the list is "<<ListLength(head)<<endl;
}
else if(operate_code==4)
{
cout<<"请输入要查找的位置i:"<<endl;
int i;
cin>>i;
cout<<"该位置的元素为:";
GetElem(head,i);
cout<<endl;
}
else if(operate_code==5)
{
int i;
cout<<"请输入线性表元素的位置,获取该位置元素的前驱元素:"<<endl;
cin>>i;
GetPre(head,i);
cout<<endl;
}
else if(operate_code==6)
{
int i;
cout<<"请输入线性表元素的位置,获取该位置元素的后继元素:"<<endl;
cin>>i;
GetNext(head,i);
cout<<endl;
}
else if(operate_code==7)
{
cout<<"请输入要插入的位置i和元素e"<<endl;
int i,e;
cin>>i>>e;
ListInsert_L(head,i,e);
}
else if(operate_code==8)
{
int i,e;
cout<<"输入要删除的位置i"<<endl;
cin>>i;
ListDelete_L(head,i,e);
cout<<endl;
}
else if(operate_code==9)
{
cout<<"The elements of the list are:"<<endl;
print(head);
cout<<endl;
}
else if(operate_code<0)
{
break;
}
else
{
cout<<"\n操作码错误!!!"<<endl;
show_help();
}
}
ClearList(head);
return 0;
}