#include<iostream.h>
#include<stdlib.h>
typedef int ElemType; //规定元素类型为整型
//定义单链表结点
struct LNode
{
ElemType data;
LNode* next;
};
//初始化单链表
void InitList(LNode* &HL)
{
HL=NULL; //置单链表为空
}
//清空单链表
void ClearList(LNode* &HL)
{
LNode *cp; //将cp指向待处理结点
LNode *np; //将np指向cp的后继结点
cp=HL;
while(cp!=NULL)
{
np=cp->next; //保存下一节点的地址
delete cp; //删除当前结点
cp=np;
}
HL=NULL;
}
//获取单链表的长度
int LenthList(LNode* HL)
{
int i=0; //存放节点数
while(HL!=NULL)
{
i++;
HL=HL->next;
}
return i;
}
//检查单链表是否为空
bool EmptyList(LNode* HL)
{
return HL==NULL;
}
//获取第pos个元素的值
ElemType GetList(LNode* HL, int pos)
{
if(pos<1)
{
cerr<<"pos的值越界!"<<endl;
exit(1);
}
int i=0;
while(HL!=NULL)
{
i++;
if(i==pos) break;
HL=HL->next;
}
if(HL!=NULL) return HL->data;
else
{
cerr<<"pos值越界!"<<endl;
exit(1);
}
}
//遍历单链表
void TraverseList(LNode* HL)
{
while(HL!=NULL)
{
cout<<HL->data<<" ";
HL=HL->next;
}
cout<<endl;
}
//查找给定值
bool FindList(LNode* HL, ElemType item)
{
while(HL!=NULL)
{
if(HL->data==item)
{
item=HL->data;
return true;
}
else HL=HL->next;
}
return false;
}
//更新单链表
bool UpdateList(LNode* HL, const ElemType item)
{
while(HL!=NULL)
{
if(HL->data==item) break;
else HL=HL->next;
}
if(HL==NULL) return false;
else
{
HL->data=item;
return true;
}
}
//插入元素
bool InsertList(LNode* &HL,ElemType item, int pos)
{
if(pos<-1)
{
cout<<"pos值无效!"<<endl;
return false;
}
//为item元素建立新节点
LNode* newptr;
newptr=new LNode;
newptr->data=item;
//寻找新节点的插入位置
LNode* cp=HL; //指向当前结点
LNode* ap=NULL; //指向cp的前驱结点
if(pos==0)
{
while(cp!=NULL)
{
if(item<cp->data) break;
else
{
ap=cp;
cp=cp->next;
}
}
}
else if(pos==-1)
{
while(cp!=NULL)
{
ap=cp;
cp=cp->next;
}
}
else
{
int i=0;
while(cp!=NULL)
{
i++;
if(i==pos) break;
else
{
ap=cp;
cp=cp->next;
}
}
if(cp==NULL && i+1<pos)
{
cout<<"pos的值超出单链表的长度加1!"<<endl;
return false;
}
}
//完成插入操作
if(ap==NULL)
{
newptr->next=HL;
HL=newptr;
}
else
{
//把新结点插入ap和cp之间
newptr->next=cp;
ap->next=newptr;
}
return true;
}
//删除元素
bool DeleteList(LNode* &HL, ElemType item, int pos)
{
//单链表为空,无法删除
if(HL==NULL)
{
cout<<"单链表为空,删除操作无效!"<<endl;
return false;
}
//判断pos是否有效
if(pos<-1)
{
cout<<"pos值无效!"<<endl;
return false;
}
//寻找被删除元素结点
LNode *cp=HL;
LNode *ap=NULL;
if(pos==0)
{
while(cp!=NULL)
{
if(cp->data==item) break;
else
{
ap=cp;
cp=cp->next;
}
}
if(cp==NULL)
{
cout<<"单链表中没有相应元素可供删除!"<<endl;
return false;
}
}
else if(pos==-1)
{
while(cp!=NULL)
{
ap=cp;
cp=cp->next;
}
}
else
{
int i=0;
while(cp!=NULL)
{
i++;
if(i==pos) break;
else
{
ap=cp;
cp=cp->next;
}
}
if(cp==NULL)
{
cout<<"pos值无效!"<<endl;
return false;
}
}
//删除cp所指向的结点
if(ap==NULL) HL=HL->next; //删除表头结点
else ap->next=cp->next;
delete cp; //收回被删除结点储存空间
return true;
}
//排序
void SortList(LNode* &HL)
{
LNode* SL; //创建一个新单链表并初始化为空
InitList(SL);
//从待排序链表中依次取出结点插入新的单链表中
LNode* r=HL;
while(r!=NULL)
{
//寻找插入位置
LNode* t=r->next;
LNode* cp=SL;
LNode* ap=NULL;
while(cp!=NULL)
{
if(r->data<cp->data) break;
else
{
ap=cp;
cp=cp->next;
}
}
//实现插入操作
if(ap==NULL)
{
r->next=SL;
SL=r;
}
else
{
r->next=cp;
ap->next=r;
}
r=t;//使r指向原单链表的下一个结点
}
HL=SL;
}
//主函数
void main()
{
int a[12]={3,6,9,12,15,18,21,24,27,30,33,36};
int i;
ElemType x;
LNode* t;
InitList(t);
for(i=0; i<12; i++)
{
InsertList(t,a[i],i+1);
}
InsertList(t,48,13);
InsertList(t,40,0);
cout<<GetList(t,4)<<' '<<GetList(t,9)<<endl;
TraverseList(t);
cout<<"输入待查找的元素值:";
cin>>x;
if(FindList(t,x)) cout<<"查找成功!"<<endl;
else cout<<"查找失败!"<<endl;
cout<<"输入待删除元素的值:";
cin>>x;
if(DeleteList(t,x,0)) cout<<"删除成功!"<<endl;
else cout<<"删除失败!"<<endl;
TraverseList(t);
cout<<"输入更新数据:";
cin>>x;
if(UpdateList(t,x)) cout<<"更新成功!"<<endl;
else cout<<"更新失败!"<<endl;
TraverseList(t);
cout<<"按值插入,输入待插入的元素的值:";
cin>>x;
if(InsertList(t,x,0)) cout<<"插入成功!"<<endl;
else cout<<"插入失败!"<<endl;
TraverseList(t);
cout<<"单链表的长度:"<<LenthList(t)<<endl;
if(EmptyList(t)) cout<<"单链表为空!"<<endl;
else cout<<"单链表不空!"<<endl;
ClearList(t);
}
运行结果: