1、查找数据是否存在
//查找数据是否存在
bool SearchList(LinkList &L,ElemType e)
{
LinkList p=L->next;//让p指向首元结点,也就是从首元结点开始比对
while(p!=NULL)//p不为空时
{
if(p->date==e)//p结点的数据域与需要查询的数据进行比对
{
return true;//如果相等,返回true
}
p=p->next;//p指向下一节点
}
return false;
}
2、查找第index个元素的值
//查找位值
ElemType SearchIndex(LinkList &L,int index)
{
LinkList p=L;//p指针指向头结点
int i=0;//i用于比对是否到达指定位置index
while (p->next!=NULL)//p指向的结点指针域不为空
{
p=p->next;//p指向下一结点
i++;
if(i==index)//到指定位置
{
return p->date;//返回此时p所指结点的数据
}
}
return -1;//查找失败
}
int main()
{
LinkList L=NULL;
InitList(L);
//查找
//查找元素是否存在
int e;
cout<<"输入需查找元素值e:";