我有一个用 c++ 实现的链表,我想在插入数据值后搜索它。用户提示要搜索的记录。但是,搜索功能没有按预期工作,它一直返回“未找到匹配项”。可能是什么问题呢?
struct node{
char name[60];
char admission[10];
char grade;
node *next;
};
node* search(node* head){
node *temp=head;
char name[60];
cout << "Enter Student to search :";
cin.ignore(10000, '\n');
cin.getline(name, 60);
cout << name;
while (temp!=NULL){
if(strcmp(temp->name, name)==0){
cout << "Match found";
return temp;
}
temp = temp->next;
}
cout << "No match found";
return NULL;
}
int main(){
node *head = NULL;
char name[60];
char admission[10];
char grade;
node *temp;
temp = (node*)malloc(sizeof(node));
int i=0;
while(i<2){
cout << "Enter students name: ";
cin.ignore(10000, '\n');
cin.getline(name, 60);
cout << "Enter student's admission number: ";
cin.getline(admission, 10);
cout << "Enter student's grade :";
cin >> grade;
strcpy(temp->name, name);
strcpy(temp->admission,admission);
temp->grade = grade;
head = temp;
i++;
}
search(head);
return 0;
}