#include<iostream>
#include<stdlib.h>
#define ERROR -40000
using namespace std;
typedef struct LNODE
{
int data;
struct LNODE *next;
}Lnode;
Lnode *Input(int n)
{
Lnode *head,*p,*q;
head=p=(Lnode*)malloc(sizeof(Lnode));
p->next=NULL;
for(int i=0;i<n;i++)
{
int data;
cin>>data;
q=(Lnode*)malloc(sizeof(Lnode));
q->data=data;
q->next=p->next;
p->next=q;
p=q;
}
return head;
}
int FindData(Lnode *L,int id)
{
Lnode *p;
p=L->next;
int j=1;
while(p!=NULL&&j<id)
{
p=p->next;
j++;
}
if(j!=id||p==NULL) return ERROR;//书上没有此处判断n+1位置输入后bug,指针没有分配内存
else return p->data;
}
int main()
{
int n;
while(cin>>n)
{
Lnode *p=Input(n);
cout<<"数据查询:"<<endl;
while(1)
{
int num;
cout<<"输入查找序号:"<<endl;
cin>>num;
int find_data=FindData(p,num);
if(find_data!=ERROR)
{
cout<<"查找结果为:"<<find_data<<endl;
}
else
{
cout<<"无法查询结果"<<endl;
}
}
}
return 0;
}


相关推荐
08-06
93
