c语言的单链表bug


//通讯录系统
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#define name_LENGTH 16
#define birthplace_LENGTH 64
#define numer_LENGTH 16
#define Email_LENGTH 256
//定义通讯录结构体的数据结构 
struct mail{
 char name[name_LENGTH];//姓名 
 char birthplace[birthplace_LENGTH];//籍贯 
 char numer1[numer_LENGTH];//号码1
 char numer2[numer_LENGTH];//号码2 
 char Email[Email_LENGTH];//电子邮箱 
};

//定义每条记录或节点的数据结构 
struct node{
 struct mail data;//数据域 
 struct node *next;//指针域 
};

//显示功能菜单 
void menu(){
printf("------------菜单-------------\n");
printf("   1 添加联系人及其信息\n");
printf("   2 通讯录记录的联系人个数\n");
printf("   3 浏览通讯录信息\n");
printf("   4 查询联系人信息\n");
printf("   5 删除联系人及其信息\n");
printf("   6 修改联系人及其信息\n");
printf("   7 退出通讯录系统\n");
printf("------------------------------\n"); 
} 
     
//显示某一联系人信息 
void show_f(struct node *q){
    printf("联系人名字:%s\n",q->data.name);
    printf("联系人籍贯:%s\n",q->data.birthplace);
    printf("联系人电话1:%s\n",q->data.numer2);
    printf("联系人电话2:%s\n",q->data.numer2);
    printf("联系人电子邮箱:%s\n",q->data.Email);     
} 

//浏览通讯录列表 
void display_f(struct node *head){
 struct node *p,*q,*min;
 char *temp1,*temp2,*temp3,*temp4,*temp5;
 p=head->next;
 if(head->next==NULL)
    printf("当前列表为空\n");
    return; 
 while(head->next!=NULL){
     min=p;
     q=p->next;
     while(q!=NULL){
        if(min->data.name[0]>p->data.name[0]){
            min=q;
            q=q->next;
        }
    }
    if(min!=p){
            strcpy(temp1,&min->data.name[0]);strcpy(&min->data.name[0],&p->data.name[0]);strcpy(&p->data.name[0],temp1);
            strcpy(temp2,&min->data.birthplace[0]);strcpy(&min->data.birthplace[0],&p->data.birthplace[0]);strcpy(&p->data.birthplace[0],temp2);
            strcpy(temp3,&min->data.numer1[0]);strcpy(&min->data.numer1[0],&p->data.numer1[0]);strcpy(&p->data.numer1[0],temp3);
            strcpy(temp4,&min->data.numer2[0]);strcpy(&min->data.numer2[0],&p->data.numer2[0]);strcpy(&p->data.numer2[0],temp4); 
            strcpy(temp5,&min->data.Email[0]);strcpy(&min->data.Email[0],&p->data.Email[0]);strcpy(&p->data.Email[0],temp5); 
        }
        p=p->next;
}
    printf("通讯录列表(按首字母排序)如下:\n");
    struct node *d_p;
    d_p=head->next;
    while(head->next!=NULL){
        show_f(d_p);
        d_p=d_p->next;    
    }
}


//联系人列表人数 
int count_f(struct node *head){
struct node *p;
 int count=0;
 p=head->next;
 while(p!=NULL){
 count++;
 p=p->next; 
 }
 return count;
}

//查询某一联系人信息
void query_f(struct node *head){
struct node *p;
p=head->next;
if(head->next==NULL){
printf("当前列表为空\n"); 
return;
}
else{
char name_lookup[name_LENGTH];//要查询的联系人的姓名 
printf("请输入你要查询的联系人姓名:\n");
scanf("%s",&name_lookup);
while(p!=NULL){
  if(p->data.name==name_lookup){
  show_f(p);
  
    
  return;
}
  else 
  p=p->next;
 }
}
 printf("当前列表无该联系人\n"); 
 return;
}


void input_f( struct node *p){
printf("请输入联系人名字:\n");
scanf("%s",&p->data.name);
printf("请输入该联系人籍贯:\n");
scanf("%s",&p->data.birthplace); 
printf("请输入该联系人电话号码1:\n");
scanf("%s",&p->data.numer1);
printf("请输入该联系人电话号码2:\n");
scanf("%s",&p->data.numer2);
printf("请输入该联系人电子邮箱:\n");
scanf("%s",&p->data.Email);  
}


//删除联系人 
void delete_f(struct node *head){
    char name_delete[name_LENGTH];
    printf("请输入要删除的联系人的姓名:\n");
    scanf("%s",name_delete); 
    struct node *p;
    p=head->next;
    if(head->next==NULL) {
    printf("当前列表为空\n");
    return;
}
    else{
    while(head->next!=NULL){
        if(strcmp(&(p->data.name[0]),name_delete)==0){
        free(p); 
        printf("删除成功\n"); 
        return;
    }
        else
        p=p->next;        
    } 
}
printf("没有找到该联系人,删除失败"); 
return; 
} 
//清空列表 
void empty_f(struct node *head) {
    struct node *p;
    p=head->next;
    if(head->next==NULL) 
    return;
    else{
    while(p!=NULL){
        free(p);
        p=p->next;
    }
printf("列表信息已全部删除");
return;    
}
}
//添加联系人及其信息 
void add_f(struct node *head){
    struct node *p,*q;
    q=head;
    p=head->next;
    struct node *node;
    node=(struct node*)malloc(sizeof(struct node));
    input_f(node);
if( head->next==NULL){ //链表为空 
head->next=node; 
}
else{
while(p!=NULL){
    if(p->data.name[0]>node->data.name[0]){//node节点应该插在p,q之间 
     node->next=q->next;
     q->next=node;
     return;
}
     else{
         q=p;
         p=p->next;
    }
}
}
q->next=node;
return;
}
    
int main(){
//创建头节点 
 struct node *head;
 head=(struct node*)malloc(sizeof(struct node)); 
 head->next=NULL;        
 int select;
 while(1>0){
     menu();
    printf("\n请输入你的选择(1~7):");
    scanf("%d",&select); 
    switch(select){ 
      case 1:add_f(head);  break;
      case 2:printf("通讯录记录的人数为:%d\n",count_f(head));  break;
      case 3:display_f(head);  break;
      case 4:query_f(head);  break;                                 
      case 5:delete_f(head);  break;
  //case 6:
      case 7:empty_f(head); printf("期待你下次使用"); return 0;
      default:printf("输入不正确\n") ; break;        
 }
}
return 0;
}

img

```

你代码有什么问题吗?

//添加联系人及其信息
void add_f(struct node *head)
{
    struct node *p, *q;
    q = head;
    p = head->next;
    struct node *node;
    node = (struct node *)malloc(sizeof(struct node));
    input_f(node);
    node->next = NULL;           //加上这行
    if (head->next == NULL)
    { //链表为空
        head->next = node;
    }
    else


代码错误比较多改了几处,可能还有


//通讯录系统
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define name_LENGTH 16
#define birthplace_LENGTH 64
#define numer_LENGTH 16
#define Email_LENGTH 256
//定义通讯录结构体的数据结构
struct mail
{
    char name[name_LENGTH];             //姓名
    char birthplace[birthplace_LENGTH]; //籍贯
    char numer1[numer_LENGTH];          //号码1
    char numer2[numer_LENGTH];          //号码2
    char Email[Email_LENGTH];           //电子邮箱
};
//定义每条记录或节点的数据结构
struct node
{
    struct mail data;  //数据域
    struct node *next; //指针域
};
//显示功能菜单
void menu()
{
    printf("------------菜单-------------\n");
    printf("   1 添加联系人及其信息\n");
    printf("   2 通讯录记录的联系人个数\n");
    printf("   3 浏览通讯录信息\n");
    printf("   4 查询联系人信息\n");
    printf("   5 删除联系人及其信息\n");
    printf("   6 修改联系人及其信息\n");
    printf("   7 退出通讯录系统\n");
    printf("------------------------------\n");
}
//显示某一联系人信息
void show_f(struct node *q)
{
    printf("联系人名字:%s\n", q->data.name);
    printf("联系人籍贯:%s\n", q->data.birthplace);
    printf("联系人电话1:%s\n", q->data.numer2);
    printf("联系人电话2:%s\n", q->data.numer2);
    printf("联系人电子邮箱:%s\n", q->data.Email);
}
//浏览通讯录列表
void display_f(struct node *head)
{
    struct node *p, *q, *min;
    char *temp1, *temp2, *temp3, *temp4, *temp5;
    p = head->next;
    if (head->next == NULL)
    {
        printf("当前列表为空\n");
        return; //放在if的{}里面
    }
    while (p->next != NULL) //head改成p
    {
        min = p;
        q = p->next;
        while (q != NULL)
        {
            if (min->data.name[0] > q->data.name[0])
            {
                min = q;
            }
            q = q->next;
        }
        if (min != p)
        {
            strcpy(temp1, &min->data.name[0]);
            strcpy(&min->data.name[0], &p->data.name[0]);
            strcpy(&p->data.name[0], temp1);
            strcpy(temp2, &min->data.birthplace[0]);
            strcpy(&min->data.birthplace[0], &p->data.birthplace[0]);
            strcpy(&p->data.birthplace[0], temp2);
            strcpy(temp3, &min->data.numer1[0]);
            strcpy(&min->data.numer1[0], &p->data.numer1[0]);
            strcpy(&p->data.numer1[0], temp3);
            strcpy(temp4, &min->data.numer2[0]);
            strcpy(&min->data.numer2[0], &p->data.numer2[0]);
            strcpy(&p->data.numer2[0], temp4);
            strcpy(temp5, &min->data.Email[0]);
            strcpy(&min->data.Email[0], &p->data.Email[0]);
            strcpy(&p->data.Email[0], temp5);
        }
        p = p->next;
    }
    printf("通讯录列表(按首字母排序)如下:\n");
    struct node *d_p;
    d_p = head->next;
    while (d_p != NULL)  //改成d_p
    {
        show_f(d_p);
        d_p = d_p->next;
    }
}

//联系人列表人数
int count_f(struct node *head)
{
    struct node *p;
    int count = 0;
    p = head->next;
    while (p != NULL)
    {
        count++;
        p = p->next;
    }
    return count;
}
//查询某一联系人信息
void query_f(struct node *head)
{
    struct node *p;
    p = head->next;
    if (head->next == NULL)
    {
        printf("当前列表为空\n");
        return;
    }
    else
    {
        char name_lookup[name_LENGTH]; //要查询的联系人的姓名
        printf("请输入你要查询的联系人姓名:\n");
        scanf("%s", &name_lookup);
        while (p != NULL)
        {
            //if (p->data.name == name_lookup)
            if( strcmp(p->data.name,name_lookup)==0 )


            {
                show_f(p);

                return;
            }
            else
                p = p->next;
        }
    }
    printf("当前列表无该联系人\n");
    return;
}

void input_f(struct node *p)
{
    printf("请输入联系人名字:\n");
    scanf("%s", &p->data.name);
    printf("请输入该联系人籍贯:\n");
    scanf("%s", &p->data.birthplace);
    printf("请输入该联系人电话号码1:\n");
    scanf("%s", &p->data.numer1);
    printf("请输入该联系人电话号码2:\n");
    scanf("%s", &p->data.numer2);
    printf("请输入该联系人电子邮箱:\n");
    scanf("%s", &p->data.Email);
}

//删除联系人
void delete_f(struct node *head)
{
    char name_delete[name_LENGTH];
    printf("请输入要删除的联系人的姓名:\n");
    scanf("%s", name_delete);
    struct node *p;
    p = head->next;
    if (head->next == NULL)
    {
        printf("当前列表为空\n");
        return;
    }
    else
    {
        while (head->next != NULL)
        {
            if (strcmp(&(p->data.name[0]), name_delete) == 0)
            {
                free(p);
                printf("删除成功\n");
                return;
            }
            else
                p = p->next;
        }
    }
    printf("没有找到该联系人,删除失败");
    return;
}
//清空列表
void empty_f(struct node *head)
{
    struct node *p;
    p = head->next;
    if (head->next == NULL)
        return;
    else
    {
        while (p != NULL)
        {
            free(p);
            p = p->next;
        }
        printf("列表信息已全部删除");
        return;
    }
}
//添加联系人及其信息
void add_f(struct node *head)
{
    struct node *p, *q;
    q = head;
    p = head->next;
    struct node *node;
    node = (struct node *)malloc(sizeof(struct node));
    input_f(node);
    node->next = NULL;           //加上这行
    if (head->next == NULL)
    { //链表为空
        head->next = node;
    }
    else
    {
        while (p != NULL)
        {
            if (p->data.name[0] > node->data.name[0])
            { //node节点应该插在p,q之间
                node->next = q->next;
                q->next = node;
                return;
            }
            else
            {
                q = p;
                p = p->next;
            }
        }
    }
    q->next = node;
    return;
}
int main()
{
    //创建头节点
    struct node *head;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;
    int select;
    while (1 > 0)
    {
        menu();
        printf("\n请输入你的选择(1~7):");
        scanf("%d", &select);
        switch (select)
        {
        case 1:
            add_f(head);
            break;
        case 2:
            printf("通讯录记录的人数为:%d\n", count_f(head));
            break;
        case 3:
            display_f(head);
            break;
        case 4:
            query_f(head);
            break;
        case 5:
            delete_f(head);
            break;
            //case 6:
        case 7:
            empty_f(head);
            printf("期待你下次使用");
            return 0;
        default:
            printf("输入不正确\n");
            break;
        }
    }
    return 0;
}

什么现象啊
name是字符串,191行要用strcmp进行name比较

53行 display_f中,if(head->next==NULL)后面的两句,需要用{}包裹起来

img

第109行 if(p->data.name==name_lookup),字符串的比较应该用strcmp
if( strcmp(p->data.name,name_lookup)==0 )