#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode *next;
}LNode, *LinkList;
LinkList List_Init(){
LinkList p = (LinkList)malloc(sizeof(LNode));
p->next = NULL;
return p;
}
LinkList Get_Elem_i(LinkList L,int i){//按给定位置查找链表中数据
//带有头结点的单链表
LinkList p = L->next;//首结点
int j = 1;//统计个数
if(i<0)
return NULL;
if(i==0)
return L;//返回头结点
while(p && j<i){
p = p->next;
j++;
}
return p;
}
LinkList Get_Elem_e(LinkList L,int e){//按给定值查找是否有此元素,有则返回,无则返回NULL
//带有头结点的单链表
LinkList p = L->next;
while(p && p->data!=e){
p = p->next; //找不到就继续查找
}
return p;//找到就返回
}
LinkList List_HeadInsert(LinkList L){
int x;
LinkList s;
scanf("%d",&x);
while(x!=9999){
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf("%d",&x);
}
return L;
}
LinkList List_TailInsert(LinkList L){
int x;
LinkList r = L;
LinkList s;
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;
return L;
}
void ListTraverse(LinkList L){
LinkList p = L->next;
while(p){
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
int main(){
LinkList L = List_Init();
List_TailInsert(L);
ListTraverse(L);
int x = 0;
scanf("%d",&x);
LinkList L1 = Get_Elem_e(L,x);
printf("%d \n",L1->data);
return 0;
}