简体   繁体   English

我的搜索功能无法正常工作,因为我的if语句出了点问题,并且我正在使用链表

[英]My search function is not working because there is something wrong with my if statement,and I'm using linked list

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

void new_contact();//add contact using linked list
void list_contact();
void delete_contact();

//int sorting_contact();//sort contact name using sorting
void search_contact();//search for contact phone number using searching

struct contact
{
    char phone_number[20];
    char contact_name[70];
    char address [80];
    struct contact *ptrnext;
};

struct contact *headptr, *newptr, *currentptr, *previousptr;

int main()
{
    system("color B");
    int key;
    char ch;
    int choice=TRUE;
    headptr=(struct contact *)NULL;

    while(choice==TRUE)
    {

        printf("\n\t\tWelcome to Smart Phone Book System");
        printf("\n\nAdd a contact :A\nList a contact :L\nDelete a contact :D"
               "\nSearch for contact :S\nSort phonebook :T\nExit :x\n");
        printf("\nPlease enter the mode you want:");
        scanf(" %c%*c", &ch);
        switch(ch)
        {
            case 'A': new_contact();system("CLS");break;

            case 'L': system("CLS");list_contact();break;

            case 'D': system("CLS");delete_contact();break;

            case 'S': search_contact();break;
            //case 'T': sorting_contact(); break;

            case 'X': choice=FALSE; break;

            default: system("CLS");printf("\nPlease enter a proper mode");
        }
    }
}

void new_contact()//add data
{
    newptr=(struct contact *)malloc(sizeof (struct contact)); // pointer to a new
                    //  memory allocation

    if (headptr==(struct contact *)NULL)//node is empty?
    {
         headptr=newptr; //first pointer point to first node
         newptr->ptrnext=(struct contact *)NULL; //first node pointer point to null
    }
    else
    {
         newptr->ptrnext=headptr;// new node pointer point to previous first node
         headptr=newptr; // head point to new node,new node become first node
    }
    printf("\nEnter a new name : ");
    fgets( newptr->contact_name,sizeof(newptr->contact_name),stdin);
    printf("\nEnter a number : ");
    fgets(newptr->phone_number,sizeof(newptr->phone_number),stdin);
    //scanf(" %d", &newptr->phone_number);
    //getchar();
    printf("\nEnter address : ");
    fgets(newptr->address,sizeof(newptr->address),stdin);
}

void list_contact()
{
    int i=1;
    if (headptr==(struct contact*)NULL) //empty list
    {
        printf("\nEmpty list");
        return;
    }

    currentptr=headptr;
    do
    {
        printf("\nContact  number %d",i);
        printf("\nName : %s", currentptr->contact_name);
        printf("\nPhone number : %s", currentptr->phone_number);
        printf("\n\nAddress : %s", currentptr->address);

        i++;
        printf("\n");
        currentptr=currentptr->ptrnext; //point to next node
    }

    while(currentptr !=(struct contact *)NULL);
}

void delete_contact()
{
    char contact_name_delete[20];

    if (headptr==(struct contact *)NULL)//node is empty?
    {
        printf("\n\nThe list is empty. Cannot delete!!!\n");
        //inform the user that the list is empty
    }
    else
    {
        printf("\nDelete list by name.");
        printf("\nEnter contact name to delete: ");
        fgets(contact_name_delete,sizeof(contact_name_delete),stdin);

        currentptr=headptr;

        while(currentptr ->ptrnext!=(struct contact *)NULL)
        {

            if (strncmp(currentptr->contact_name,contact_name_delete,6)==0) 
            //found the location
            {
                break;
            }
            else
            {
                previousptr=currentptr;//save the previous current address
                currentptr=currentptr->ptrnext; 
                //point to next node
            }
        }

        if (strncmp(currentptr->contact_name,contact_name_delete,6)==0)
        {
            if ( currentptr==headptr) //number is the first and only node in list
            {
                headptr=currentptr ->ptrnext; //head point to NULL
                free(currentptr);
            }
            else //delete at the middle of link list
            {
                previousptr->ptrnext=currentptr->ptrnext;
                free(currentptr);//destroy  node, free the memory.
            }
            printf("\nPhone number were deleted!\n");
        }
        else
            printf("\nNumber to be deleted is not in the list!!! ");
    }
}

My problem is in the search_contact() function: 我的问题是在search_contact()函数中:

void  search_contact()//The function for searching the contact based on name
{
    currentptr=headptr;
    char contact_name_add[30];
    if (headptr ==(struct contact *)NULL)
    {
        printf("The phonebook is empty..");
    }
    else
    {

        printf("Please enter the name of your contact for searching : ");//user inputs name for searching
        fgets(contact_name_add,sizeof(contact_name_add),stdin);//uses linear search concept

        printf("\n\nThe computer will now search for your contact");

        do
        {
            if (strncmp(currentptr->contact_name,contact_name_add,5)==0)
            {
                printf("\nName :         %s", currentptr->contact_name);
                printf("\nPhone Number : %s",currentptr->phone_number);
                printf("\nAddress :      %s", currentptr->address);
                currentptr=currentptr->ptrnext;
            }
            else
            {
                currentptr=currentptr->ptrnext;

I've found out that the line below is my problem, but I don't know why 我发现下面的行是我的问题,但我不知道为什么

                if (currentptr->ptrnext=NULL
                    && strncmp(currentptr->contact_name,contact_name_add,5)!=0)
                {
                    printf("\nThe name you seek is not in the phonebook~~");
                }
            }
        } while(currentptr!=(struct contact *)NULL);
    }
}

I can search my contact that I've inserted just fine, but the problem exists when I have 2 or more contacts. 我可以搜索已插入的联系人,但是当我有2个或更多联系人时,就会出现问题。 The search function is quite similar to my delete function, but I've coded it to display more that 1 contacts (in the case of similar contact names), but when it reaches NULL, it gives out an error, and I could not find it on Google (my bad on that part). 搜索功能与删除功能非常相似,但我对其进行了编码,以显示多于1个联系人(在相似的联系人姓名的情况下),但是当它达到NULL时,它会发出错误消息,并且我找不到它在Google上(我不好的一面)。

尝试使用以下IF:

if(currentptr->ptrnext==NULL && strncmp(currentptr->contact_name,contact_name_add,5)!=0)

Don't do assignments in your if statements! 不要在if语句中进行赋值! The code reading: 代码阅读:

if (currentptr->ptrnext=NULL

should be: 应该:

if (currentptr->ptrnext == NULL

With the assignment (single = ), the result of the assignment is false, so the second half of the conditional — the strncmp() — is never executed. 使用赋值(single = ),赋值的结果为false,因此永远不会执行条件的strncmp()

If you use GCC, you should be using options so that it warns you about such problems — -Wall would be sufficient for that warning. 如果使用GCC,则应使用选项,以便它警告您此类问题-Wall对于该警告是足够的。 I routinely use this (or -std=c11 and the other options): 我通常使用它(或-std=c11和其他选项):

gcc -g -O3 -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
    -Wold-style-definition -Wold-style-declaration -Werror ...

Your code would not compile under those options. 您的代码将无法在这些选项下进行编译。


Given the claim made in comments that the assignment is a typo, then the rest of the problem is the logic. 鉴于评论中声称作业是错字,那么剩下的问题就是逻辑了。 The loop as currently written is, when presented more legibly: 当更清晰地呈现当前编写的循环时:

    do
    {
        if (strncmp(currentptr->contact_name, contact_name_add, 5) == 0)
        {
            printf("\nName :         %s", currentptr->contact_name);
            printf("\nPhone Number : %s", currentptr->phone_number);
            printf("\nAddress :      %s", currentptr->address);

            currentptr = currentptr->ptrnext;
        }
        else
        {
            currentptr = currentptr->ptrnext;

            if (currentptr->ptrnext == NULL &&
                strncmp(currentptr->contact_name, contact_name_add, 5) != 0)
            {
                printf("\nThe name you seek is not in the phonebook~~");
            }
        }
    } while (currentptr != (struct contact *)NULL);

I don't trust bottom-testing loops; 我不相信底部测试循环; there are occasions when you need to go at least once through a loop, but lists can be empty and then you don't want to go through the body of the loop at all. 在某些情况下,您至少需要循环执行一次,但是列表可以为空,因此您根本不想通过循环的正文。

I'd probably use: 我可能会使用:

    while (currentptr != NULL)
    {
        if (strncmp(currentptr->contact_name, contact_name_add, 5) == 0)
        {
            printf("Name :         %s\n", currentptr->contact_name);
            printf("Phone Number : %s\n", currentptr->phone_number);
            printf("Address :      %s\n", currentptr->address);
            break;
        }
        currentptr = currentptr->ptrnext;
    }
    if (currentptr == NULL)
        printf("The name you seek (%s) is not in the phonebook\n", contact_name_add);

You might note that I end lines of output with a newline; 您可能会注意到,我以换行符结束输出行; I don't usually begin lines of output with a newline unless I want double spacing. 除非我想要双倍行距,否则我通常不以换行开始输出行。 Your input code needs to be fixed to remove the newlines that fgets() keeps. 您的输入代码需要修复,以删除fgets()保留的换行符。 Write a function to call fgets() and strip the newline from the input. 编写一个函数来调用fgets()并从输入中删除换行符。

[Can you] modify the loop and give the search result in case of multiple same contact name? [您是否可以修改循环并在有多个相同联系人姓名的情况下给出搜索结果?

Of course. 当然。 It is software; 它是软件; you can always change the spec and then change the code. 您可以随时更改规格,然后更改代码。 You lose the break in the loop and count the matches and only report 'no match' if the count is zero after the loop. 您会在循环中丢失中断并计算匹配数,并且如果在循环后计数为零,则仅报告“不匹配”。

To print all matches: 要打印所有匹配项:

    int counter = 0;
    while (currentptr != NULL)
    {
        if (strncmp(currentptr->contact_name, contact_name_add, 5) == 0)
        {
            printf("Name :         %s\n", currentptr->contact_name);
            printf("Phone Number : %s\n", currentptr->phone_number);
            printf("Address :      %s\n", currentptr->address);
            ++counter;
        }
        currentptr = currentptr->ptrnext;
    }
    if (counter == 0)
        printf("The name you seek (%s) is not in the phonebook\n", contact_name_add);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在创建 function 以在我的链接列表中搜索字符串时遇到问题 - I'm having trouble creating a function to search for a string in my linked list 为什么我的删除搜索链表 function 不起作用? - why does my delete search linked list function not working? 简化我的链接列表“搜索和删除”功能 - Simplify my Linked list “search and delete” function 我使用链表(没有数组)创建了一个堆栈,但是我的 pop 函数不起作用 - I made a stack using linked list (without array), but my pop function is not working 我在这个 C 扩展中引用我的实例的方式有什么问题吗? - Is there something wrong with how I'm referencing my instances in this C extension? 搜索 function 链表中的 if 语句 - if statement in search function linked list 为什么我在链表中​​的附加功能不起作用? - why my append function in linked list not working? 为什么我的打印链表 function 不起作用? - Why is my print linked list function not working? 我在C中实现链接列表,我的addhead功能无法正常工作。 怎么了? - I am implementing linked lists in C, and my addhead function is not working correctly. What's wrong? 我对链接列表中的名字进行计数的功能有什么问题? - What is wrong with my function for counting names in a linked list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM