#include <stdio.h>
#include <stdlib.h>
//单链表头插
//定义节点类型
typedef struct LNode {
int data;//数据域
struct LNode *next;//指针域
} LNode, *LinkList;
void headList(LinkList &l) {//LinkList等价LNode*(结构体指针)
l = (LinkList) malloc(sizeof(LNode));
l->next = NULL;
int x;
scanf("%d", &x);
LinkList s;
while (x != 9999) {
s = (LinkList) malloc(sizeof(LNode));
s->data = x;
//最后两步顺序不能换
s->next = l->next;
l->next = s;
scanf("%d", &x);
}
}
void tailList(LinkList &l) {
l = (LinkList) malloc(sizeof(LNode));
l->next = NULL;
int x;
scanf("%d", &x);//s指向新节点,r指向链表尾
LinkList s, r = l;
while (x != 9999) {
s = (LinkList) malloc(sizeof(LNode));//s存储了这个节点的起始地址.s指向此节点
s->data = x;
r->next = s;//新节点给尾节点next指针
r = s;//r指向新的尾部
scanf("%d", &x);
}
r->next = NULL;
}
void printList(LinkList l) {
l = l->next;
while (l != NULL) {
printf("%3d", l->data);
l = l->next;
}
}
LinkList getByLoxation(LinkList l, int pos) {
int i = 1;
l = l->next;
while (l && i < pos) {
l = l->next;
i++;
}
return l;
}
LinkList getByValue(LinkList l, int value) {
l = l->next;
while (l) {
if (l->data == value) {
return l;
} else {
l = l->next;
}
}
return l;
}
int main() {
LinkList l;
LinkList search;
LinkList searchValue;
// headList(l);
// printList(l);
tailList(l);
// search = getByLoxation(l, 2);
// printf("%d", search->data);
searchValue = getByValue(l, 3);
printf("%d", searchValue->data);
// printList(l);
return 0;
}