我想检查重复添加相同姓名的情况,可这个代码运行有问题,可以帮忙看看问题出在哪里了吗
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student
{
char name[20];//名字
char wm[20];//性别
char work[100];//工作单位
char stel[20];//手机
struct student *next;
}stu;
stu *head;
void input()
{
int ans;
stu *p1,p2;
p1=(stu)malloc(sizeof(stu));
if(p1!=NULL)
{
printf("========输入数据========\n");
head=p1;
while(1)
{
printf("名字:");
scanf("%s",&p1->name);
stu* tmp = head;
while (tmp != NULL) {
if (strcmp(tmp->name, p1->name) == 0) {
printf("该名字已经存在,请重新输入!\n");
continue;
}
tmp = tmp->next;
}
printf("性别:");
scanf("%s",&p1->wm);
printf("工作单位:");
scanf("%s",&p1->work);
printf("手机:");
scanf("%s",&p1->stel);
printf("===================================\n");
p2=p1;
p1=(stu*)malloc(sizeof(stu));
if(p1!=NULL)
p2->next=p1;
printf("请选择是否继续输入:1.继续 2.退出\n请选择:");
scanf("%d",&ans);
if(ans==1)
continue;
else//退出
{
printf("========输入完毕========\n");
p2->next=NULL;
free(p1);
break;
}
}
}