首页 > 解决方案 > 这个 c 程序运行没有错误,但搜索、删除、更新功能不起作用

问题描述

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct Node
{
    char firstname[100];
    char lastname[100];
    char number[100];
    char mail[100];
    struct Node *next;
}*head;

void insert(char* firstname,char* lastname,char* number,char* mail)
{
    struct Node * node=(struct Node *)malloc(sizeof(struct Node));
    strcpy(node->firstname, firstname);
    strcpy(node->lastname, lastname);
    strcpy(node->number, number);
    strcpy(node->mail, mail);
    node->next=NULL;
    if(head==NULL)
    {
        head=node;
    }
    else{
        node->next=head;
        head=node;
    }
    
}
void search(char* firstname)
{
    struct Node * temp = head;
    while(temp!=NULL){
        if(temp->firstname==firstname){
        printf("Contact Found");
        printf("Firstname:%s\n",temp->firstname);
        printf("Lastname:%s\n",temp->lastname);
        printf("PhoneNumber:%s\n",temp->number);
        printf("Mail Id:%s\n",temp->mail);
        return;
        }  
        temp = temp->next;
    }
    printf("%s is not found in the contact \n",firstname);
}
void update(char* firstname)
{
    struct Node * temp=head;
    while(temp!=NULL){
        if(temp->firstname==firstname){
          printf("Contact Found");
          printf("Enter the new Phone number for %s\n",temp->firstname);
          scanf("%s",temp->number);
          printf("Contact Updated Successfully\n");
          return;
        }
        temp=temp->next;
    }
    printf("%s is not found in the contact \n",firstname);
}
void delete(char* firstname)
{
    struct Node * temp1 = head;
    struct Node * temp2 = head; 
    while(temp1!=NULL){
        if(temp1->firstname==firstname){
          printf("Contact Found for deleting\n");
          if(temp1==temp2){
              head = head->next;
              free(temp1);
          }
          else{
              temp2->next = temp1->next;
              free(temp1);
          }
          printf("Contact deleted Successfully\n");
          return;
        }
        temp2=temp1;
        temp1=temp1->next;
    }
    printf("%s is not found in the contact \n",firstname);
}
void display()
{
    struct Node * temp=head;
    while(temp!=NULL){
        printf("Firstname:%s\n",temp->firstname);
        printf("Lastname:%s\n",temp->lastname);
        printf("PhoneNumber:%s\n",temp->number);
        printf("Mail Id:%s\n",temp->mail);
        temp = temp->next;
        
    }   
}
int main()
{
    head = NULL;
    int choice;
    char firstname[100];
    char lastname[100];
    char number[100];
    char mail[100];
    printf("-------Welcome--------\n ");
    printf("1.Insert a Contact\n2.Search a Contact\n3.Delete a Contact\n4.Update a Contact\n5.Display all the Contacts");
    do
    {
        printf("\nEnter Choice: ");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                printf("Enter Firstname:");
                scanf("%s",firstname);
                printf("Enter Lastname:");
                scanf("%s",lastname);
                printf("Enter PhoneNumber:");
                scanf("%s",number);
                printf("Enter Mail Id:");
                scanf("%s",mail);
                insert(firstname,lastname,number,mail);
                break;
            case 2:
                printf("Enter Firstname to Search:");
                scanf("%s",firstname);
                search(firstname);
                break;
            case 3:
                printf("Enter Firstname to Delete:");
                scanf("%s",firstname);
                delete(firstname);
                break;
            case 4:
                printf("Enter Firstname to Update:");
                scanf("%s",firstname);
                update(firstname);
                break;
            case 5:
                display();
                break;
        }
        
    }while (choice != 0);
    
}

这个 c 程序运行没有错误,但搜索、删除、更新功能不起作用...您可以参考 img 了解更多详细信息。明天我必须提交我的迷你项目..所以如果有人知道 c 程序请帮我输入选择:2
输入名字以搜索:durai
durai 在联系人中找不到

Enter Choice: 3
Enter Firstname to Delete:durai
durai is not found in the contact

输入选择:4
输入名字以更新:durai
durai 在联系人中找不到这些是我得到的错误

标签: ccomparisonsingly-linked-listc-stringsfunction-definition

解决方案


例如,在函数search中,您尝试比较指向不同内存范围的两个指针

if(temp->firstname==firstname){

因此,即使指向的字符串彼此相等,条件也会计算为假。您必须使用标准的字符串函数strcmp

例如

if( strcmp( temp->firstname, firstname ) == 0 ){

请注意,所有具有 type 的函数参数char *都应声明为具有 type const char *

当函数依赖于全局变量时也是一个坏主意head。例如,在这种情况下,您不能在一个程序中创建多个列表。


推荐阅读