#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
LinkList creat()
{
LinkList L;
L = (LinkList)malloc(sizeof(LNode));
if(!L) printf("memory malloc error!\n");
LinkList p = L;
ElemType a;
int len;
printf("请输入待建表的表长:");
scanf("%d", &len);
for(int i=0; i<len; i++)
{
printf("请输入第%d个元素的值:", i+1);
scanf("%d", &a);
p->next = (LinkList)malloc(sizeof(LNode));
p->next->data = a;
p = p->next;
}
p->next = NULL;
return L;
}
void print(LinkList L)
{
LinkList p = L->next;
while(p != NULL)
{
printf("%d\n", p->data);
p = p->next;
}
}
LinkList LOCATE(LinkList L, ElemType X)
{
if(L == NULL) return NULL;
else
{
LinkList p = L->next;
while(p)
{
if(p->data == X)
return p;
else
p = p->next;
}
}
}
void main()
{
LinkList L = creat();
int x;
printf("请输入待查找的元素值:");
scanf("%d", &x);
LinkList p = LOCATE(L, x);
if(p)
printf("%d", *p);
else
printf("查找失败!");
}