这个函数有错么????(查找链表中元素,并输出所在位置
这个函数有错么????怎么改???int Locate(NODE *head, NODE *data){
NODE *p;
int n;
for (p = head, n = 0; p->link != NULL;){
if (strcmp(p->name,data->name))return n;
else p = p->link; n++;
}
}
目的:查找链表中元素,并输出所在位置
如果没错的话那是不是调用的语句错了???下面是完整代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char name[20];
struct node *link;
}NODE;
void creat(NODE *head, int n){
NODE *p;
for (; n > 0; n--){
p = (NODE *)malloc(sizeof(NODE));
gets(p->name);
p->link = NULL;
head->link = p;
head = p;
}
}
void output(NODE *head){
NODE *p;
p = head->link;
while (p != NULL){
puts(p->name);
p = p->link;
}
}
int Locate(NODE *head, NODE *data){
NODE *p;
int n;
for (p = head, n = 0; p->link != NULL;){
if (strcmp(p->name,data->name))return n;
else p = p->link; n++;
}
}
void main(){
NODE *head;
int n;
head = (NODE *)malloc(sizeof(NODE));
head->link = NULL;
printf("结点个数为:");
scanf("%d", &n);
getchar();
creat(head, n);
printf("\n\n");
printf("输出;\n");
output(head);
NODE *p;
p = (NODE *)malloc(sizeof(NODE));
printf("请输入要查找的数据:");
gets(p->name);
printf("所在位置为:%d", Locate(head, p));
}