按值查找和按位查找,按位查找有点不足。但能够使用
#include <stdio.h>
#include <stdlib.h>
typedef int elemType;
typedef struct LNode{
elemType data;
struct LNode *next;
}LNode,*LinkList;
//尾插法新建链表
void list_tail_insert(LinkList &L)
{
elemType x;
L= (LinkList)malloc(sizeof(LNode));
L->next=NULL;
LinkList s;//s指针是用来指向申请的新结点
LinkList r=L;//r始终指向链表尾部
scanf("%d",&x);
while (x!=9999)
{
s=(LinkList)malloc(sizeof(LNode));//给新结点申请空间
s->data=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
}
void print_list(LinkList L)
{
L=L->next;
while (L!=NULL)
{
printf("%3d",L->data);
L=L->next;
}
printf("\n");
}
LinkList getElem(LinkList L,elemType searchPos)
{
int i=1;
LinkList p=L->next;
while (p&&i<searchPos)
{
p=p->next;
i++;
}
return p;
}
int getValue(LinkList L,elemType searchValue)
{
int length=10;
LinkList p=L->next;
for (int i = 0; i < length; i++)
{
if(p->data=searchValue)
{
return p->data;
}
p=p->next;
}
}
int main() {
LinkList L,search;
list_tail_insert(L);
print_list(L);
search=getElem(L,1);
if(search)
{
printf("查找成功,且对应位置的值=%d\n",search->data);
}
printf("\n--------------------------\n");
int value;
value=getValue(L,6);
if(search)
{
printf("search success %d\n",value);
}
return 0;
}