c++链表问题。为什么我输入1,给我的答案是Miss.高手谢谢!

来源:百度知道 编辑:UC知道 时间:2024/09/25 07:35:50
#include<iostream.h>
#include <afxtempl.h>

struct node{
int key;
node *link;
};
node *first;
node *p=first;

struct address{
node *Address;
address *q;
};

address *afirst;
address *ap=afirst;

find(int a,int arr[],int m)
{
int flag=0;
while(p!=NULL)
{
if(p->key==a)
{
ap->q=new address;
ap->q->Address=p;
ap=ap->q;
ap->q=NULL;
flag=1;
}
p=p->link;
}
return flag;
}
void main()
{
int a,i,h,arr[5];
cout<<"please enter the value for search!"<<endl;
cin>>a;
for(i=0;i<5;i++)
arr[i]=i;
h=find(a,arr,5);
if(h)
cout<<"Match!"<<endl;
else cout<<"Miss"<<endl;
}

find函数中是在对链表进行操作,但是函数的参数确实数组名;函数find的参数arr【】在函数体中根本就没有用到。
还有一个问题,既然在find函数中要求用链表来操作,那么在主函数中就应该对链表进行初始化,将链表的头指针作为函数find的参数。如果用数组名作为函数参数,在find中还要去初始化链表,纯粹多此一举。
如果能够用不做结构体的嵌套,尽量不要用。

全局变量结构指针在未赋值指向0地址。