Liu y0101 2023-06-10 23:30 采纳率: 66.7%
浏览 22
已结题

关于#链表#的问题:我想检查重复添加相同姓名的情况(语言-c语言)

我想检查重复添加相同姓名的情况,可这个代码运行有问题,可以帮忙看看问题出在哪里了吗

#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;
        }
    }
}


  • 写回答

3条回答 默认 最新

  • qzjhjxj 2023-06-11 01:13
    关注

    修改如下,改动处见注释,供参考:

    #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 = NULL;
    void input()
    {
        int ans;
        stu *p1, *p2, *tmp; //stu *p1,p2;   修改
        printf("========输入数据========\n");
        while(1)
        {
            p1=(stu*)malloc(sizeof(stu)); // 修改
            p1->next = NULL;              // 修改
    
            printf("名字:");
            scanf("%s",p1->name);   // scanf("%s",&p1->name); 修改
            getchar();
            if (head) {
                tmp = head;
                while (tmp != NULL) {
                    if (strcmp(tmp->name, p1->name) == 0) {
                        printf("该名字已经存在,请重新输入!\n");
                        free(p1);        // 修改
                        break;           //continue; 修改
                    }
                    tmp = tmp->next;
                }
                if (tmp)  continue;      // 修改
            }
            printf("性别:");
            scanf("%s",p1->wm);    //scanf("%s",&p1->wm); 修改
            getchar();
            printf("工作单位:");
            scanf("%s",p1->work); //scanf("%s",&p1->work); 修改
            getchar();
            printf("手机:");
            scanf("%s",p1->stel); //scanf("%s",&p1->stel); 修改
            getchar();
            printf("===================================\n");
            if (!head)     //if(p1!=NULL) 修改
                head = p1;
            else
                p2->next = p1;
            p2 = p1;
    
            do {    // 修改
                printf("请选择是否继续输入:1.继续  2.退出\n请选择:");
                scanf("%d",&ans);
            }while (ans < 1 || ans > 2);// 修改
                              //continue;  修改
            if(ans == 2)      //else//退出 修改
            {
                printf("========输入完毕========\n");
                //p2->next=NULL;  修改
                //free(p1);       修改
                break;
            }
        }
    }
    void print(stu *P)
    {
        while (P){
            printf("%s %s %s %s\n",P->name,P->wm,P->work,P->stel);
            P = P->next;
        }
    }
    
    int main()
    {
        input();
        print(head);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月19日
  • 已采纳回答 6月11日
  • 修改了问题 6月10日
  • 创建了问题 6月10日