【问题标题】:making a phonebook system using linked list使用链表制作电话簿系统
【发布时间】:2019-12-26 16:51:39
【问题描述】:

我正在使用单链表在 c 中创建一个电话簿系统,似乎一切正常,但删除选项总是给我一个错误,我不知道如何修复它,所以我希望有人能告诉我是什么代码中的问题,给我看一个可以修改此代码中名称或数字的代码

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

struct node
{
    char firstname[20];
    char lastname[20];
    long int number;
    struct node *next;
};

struct node *head=NULL;

struct node *getnode()
{
    return((struct node *)malloc(sizeof(struct node)));
}


void display(struct node *head)
{
    struct node *temp;
    temp=head;
    while(temp!=NULL)
    {
        printf("%s\n",temp->firstname);
        printf("%s\n",temp->lastname);
        printf("%d\n",temp->number);
        temp=temp->next;
    }
}

void insert()
{
    struct node *temp,*newnode;
    newnode=getnode();
    temp=head;
    while(temp->next!=NULL)
    {
        temp=temp->next;

    }
    printf("Enter First name:\n");
    scanf("%s",&newnode->firstname);
    printf("Enter Last name:\n");
    scanf("%s",&newnode->lastname);
    printf("Enter number:\n");
    scanf("%d",&newnode->number);
    temp->next=newnode;
    newnode->next=NULL;
    display(head);
}
struct node *create()
{
    struct node *temp,*newnode;
    if(head!=NULL)
        insert();
    else
    {
    newnode=getnode();
    head=newnode;
    temp=head;
    printf("Enter First name:\n");
    scanf("%s",&newnode->firstname);
    printf("Enter Last name:\n");
    scanf("%s",&newnode->lastname);
    printf("Enter number:\n");
    scanf("%d",&newnode->number);
    newnode->next=NULL;
    display(head);
    }
}
void search()
{
    struct node *temp;
    char *first,*last;
    temp=head;
    printf("Enter name to be searched:\n");
    scanf("%s",&first);
    scanf("%s",&last);
    while((temp->firstname==first)&&(temp->lastname==last))
    {
        temp=temp->next;
    }
    printf("%s\n",temp->firstname);
    printf("%s\n",temp->lastname);
    printf("%d\n",temp->number);
}

void del()
{
    struct node *pretemp,*temp;
    char *f,*l;
    temp=head;
    pretemp=head->next;
    printf("Enter name :");
    scanf("%s",&f);
    scanf("%s",&l);
        while(temp!=NULL){
        if((pretemp->firstname==f)&&(pretemp->lastname==l))
        {
            printf("%s ",temp->firstname);
            printf("%s ",temp->lastname);
            printf("%s ",temp->number);
            temp=pretemp->next;
            delete pretemp;
            break;
        }
         else
        {
            temp=temp->next;
            pretemp=pretemp->next;
        }

}       
int main()
{
    int op,ch;
    do{
        printf("-------Welcome--------\n");
        printf("1.Create\n2.Display\n3.Delete\n4.Search\n");
        printf("Enter your choice:");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: create();
            break;
            case 2: display(head);
            break;
            case 3: del();
            break;
            case 4:search();
            break;

        }
        printf("Do you want to quit ? 1 for no / 0 for yes:");
        scanf("%d",&op);
    }while(op);
return 0;
}

this is the error

【问题讨论】:

  • 请分享错误信息
  • delete pretemp;
  • 尝试附加调试器,这可能有助于您确定错误情况
  • 您的代码似乎无法编译。无论如何,现在是学习如何调试程序的好时机。看看这里 - stackoverflow.com/questions/2590931/how-to-debug-a-c-program 您很可能由于缓冲区溢出或错误指针而访问无效内存。请编辑源代码并提供实际编译的minimal reproducible example
  • 如果您正在编写 delete pretemp;,那么您是在使用 C++ 而非 C 进行编码。但是(显然)您正在使用 malloc(在 struct node *getnode 中)分配内存。您不应该混合 C(malloc/free)和 C++(new/delete)内存分配。我建议您选择其中一个。

标签: c data-structures


【解决方案1】:

我对您的搜索和删除功能进行了以下更改

  1. 搜索和删除函数中的第一个和最后一个缓冲区在 scanf 中使用之前未分配内存。这会导致运行时错误
  2. 您在 scanf 中捕获用户输入的名字和姓氏的方式也不正确
  3. 在搜索和删除功能中,我修改了字符串比较。要与字符串进行比较,您需要使用 strncmp 函数。使用== 将检查第一个字节的地址。
  4. 在搜索功能中,您没有检查列表末尾。
  5. 在 del 函数中,我已将 printf("%s ", temp-&gt;number) 更改为 printf("%d ", temp-&gt;number)

void search()
{
    struct node *temp;
    char first[20], last[20];
    temp = head;
    printf("Enter name to be searched:\n");
    scanf("%s", first);
    scanf("%s", last);
    while (temp != NULL && ((strncmp(temp->firstname,  first, 20) != 0) && (strncmp(temp->lastname, last, 20) != 0)))
    {
        temp = temp->next;
    }
    if (temp != NULL) {
        printf("%s\n", temp->firstname);
        printf("%s\n", temp->lastname);
        printf("%d\n", temp->number);
    }
}

void del()
{
    struct node *pretemp, *temp;
    char first[20], last[20];
    temp = head;
    pretemp = head->next;
    printf("Enter name :");
    scanf("%s", first);
    printf("Enter Last name:");
    scanf("%s", last);
    while (temp != NULL) {
        if((strncmp(temp->firstname, first, 20) != 0) && (strncmp(temp->lastname, last, 20) != 0))
        {
            printf("%s ", temp->firstname);
            printf("%s ", temp->lastname);
            printf("%d ", temp->number);
            temp = pretemp->next;
            delete pretemp;
            break;
        }
        else
        {
            temp = temp->next;
            pretemp = pretemp->next;
        }

    }
}

【讨论】:

  • 我尝试了你的更改,但仍然有同样的错误,编译器总是在 pretemp = pretemp->next 上放一条蓝线
【解决方案2】:

我对您的代码进行了一些更改,您可以在下面的代码中找到注释。 它在 linux ubuntu 18.04 下编译和比较,现在可以使用。 基本上,当您使用 scanf(" %s ", asr) 时,'astr' 应该有足够的空间来接受输入。

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

struct node
{
    char firstname[20];
    char lastname[20];
    long int number;
    struct node *next;
};

struct node *head=NULL;

struct node *getnode()
{
    return((struct node *)malloc(sizeof(struct node)));
}


void display(struct node *head)
{
    struct node *temp;
    temp=head;
    while(temp!=NULL)
    {
        printf("%s\n",temp->firstname);
        printf("%s\n",temp->lastname);
        printf("%ld\n",temp->number);  /* number is long int */
        temp=temp->next;
    }
}

void insert()
{
    struct node *temp,*newnode;
    newnode=getnode();
    temp=head;
    while(temp->next!=NULL)
    {
        temp=temp->next;

    }
    printf("Enter First name:\n");
    scanf("%s",newnode->firstname);
    printf("Enter Last name:\n");
    scanf("%s",newnode->lastname);
    printf("Enter number:\n");
    scanf("%ld",&newnode->number);
    temp->next=newnode;
    newnode->next=NULL;
    display(head);
}
struct node *create()
{
    struct node *temp,*newnode;
    if(head!=NULL)
        insert();
    else
    {
    newnode=getnode();
    head=newnode;
    temp=head;
    printf("Enter First name:\n");
    scanf("%s",newnode->firstname);
    printf("Enter Last name:\n");
    scanf("%s",newnode->lastname);
    printf("Enter number:\n");
    scanf("%ld",&newnode->number);
    newnode->next=NULL;
    display(head);
    }
}
void search()
{
    struct node *temp;
    char first[20], last[20]; /* space for input */
    temp=head;
    printf("Enter name to be searched:\n");
    scanf("%s",first);  /* you dont need '&' operator for string*/
    scanf("%s",last);
    while((temp->firstname==first)&&(temp->lastname==last))
    {
        temp=temp->next;
    }
    printf("%s\n",temp->firstname);
    printf("%s\n",temp->lastname);
    printf("%ld\n",temp->number); /* number is long int */
}

void del()
{
    struct node *pretemp,*temp;
    char f[20],l[20];   /* you need a space to store input */
    temp=head;
    pretemp=head->next;
    printf("Enter name :");
    scanf("%s",f);   /* you dont need '&' operator to access a string */
    scanf("%s",l);
    while(temp!=NULL){

        if((pretemp->firstname==f)&&(pretemp->lastname==l))
        {
            printf("%s ",temp->firstname);
            printf("%s ",temp->lastname);
            printf("%ld ",temp->number); /* 'number' is long int */
            temp=pretemp->next;
             free(pretemp);  /* 'delete' is c++ operator, not C */
            break;
        }
         else
        {
            temp=temp->next;
            pretemp=pretemp->next;
        }
    } /* missing curly bracket */
}
int main()
{
    int op,ch;
    do{
        printf("-------Welcome--------\n");
        printf("1.Create\n2.Display\n3.Delete\n4.Search\n");
        printf("Enter your choice:");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: create();
            break;
            case 2: display(head);
            break;
            case 3: del();
            break;
            case 4:search();
            break;

        }
        printf("Do you want to quit ? 1 for no / 0 for yes:");
        scanf("%d",&op);
    }while(op);
return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多