首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用then deleting语句后,C链表不再起作用

使用then deleting语句后,C链表不再起作用
EN

Stack Overflow用户
提问于 2013-12-16 13:09:48
回答 1查看 73关注 0票数 2

我在这里有一个程序来显示输入日期的下一周的某一天。这不是一个实用或高效的程序,我只是用它来测试我学到的概念。它工作了一段时间,但后来在学习了type语句后,我尝试使用它们作为程序结构的“工作日”来声明该类型的变量,但随后删除了type,因为它产生了错误(例如“不兼容的指针”和“不完整的定义”)。现在,即使我去掉了typedef语句,文件也会显示错误。最奇怪的是,如果我将所有代码复制并粘贴到一个新文件中,错误就不会出现。有没有人知道可能是什么导致了这些错误?

代码语言:javascript
复制
#include <stdio.h>
#include <stdbool.h>

    struct weekday {
        char *ptrday;
        struct weekday *next;
    };

void matchtest(char *eday, struct weekday *head, struct weekday *cursor) {
    cursor=head;
    bool equalstring=true;

    while (cursor!=NULL) {
        char *p=cursor->ptrday;
        equalstring=true;

        while (*eday!='\0') {
            if (*eday!=*p) {
                equalstring=false;
                break;
            }
            ++eday; ++p;
        }
        if (equalstring==1) {
            printf("The next day of the week is %s\n", cursor->next->ptrday);
            break;
        }
        cursor=cursor->next;
    }
    if (equalstring==false)
        printf("The next day of the week is Sunday\n");
}

int main (void) {
    char enteredday[80], *ptreday=enteredday;
    struct weekday sunday, monday, tuesday, wednesday, thursday, friday, saturday;
    struct weekday *head=&sunday;
    struct weekday *cursor=NULL;

    sunday.ptrday="Sunday"; monday.ptrday="Monday"; tuesday.ptrday="Tuesday";
        wednesday.ptrday="Wednesday"; thursday.ptrday="Thursday";
        friday.ptrday="Friday"; saturday.ptrday="Saturday";

    sunday.next=&monday; monday.next=&tuesday; tuesday.next=&wednesday;
        wednesday.next=&thursday; thursday.next=&friday; friday.next=&saturday;
        saturday.next=NULL;


    printf("This is a test for the next day of the week.\n\n");
    printf("Enter a day ");

    scanf("%s", enteredday);
    matchtest(ptreday, head, cursor);

    return 0;
}
EN

回答 1

Stack Overflow用户

发布于 2013-12-16 13:44:51

您遇到的一个问题是您在比较器函数中修改了eday,但是如果比较失败,您将无法将搜索设置为从字符串的开头开始,因为您不知道字符串从哪里开始。这困扰着“星期四”,它报道“第二天”是星期天。对于其他一些测试,这可以正常工作。据报道,周六之后的第二天是周日,因为这件事很繁琐。

你也有一个问题,周六之后的一天是不确定的。

下面是可以正常工作的代码。我使用strcmp() (因此也使用<string.h> )进行比较。我将while (cursor != NULL)循环转换为do { ... } while (cursor != head);循环,以便循环链表正常工作。我还在读取输入时打印它,并在正式输出中再次打印。这允许我使用bash的游标表示法( Here String notation,<<< string)进行测试,这很方便。请注意,输入检查EOF值,并将输入字符串限制为79个字符,以防止缓冲区溢出(也称为堆栈溢出)。函数的‘Here String’参数是不必要的;它在调用代码中设置为null,但被调用的函数立即将其设置为head。因此该参数不再存在,但仍然需要该变量,因此它对于函数来说是纯局部的。

代码

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>

struct weekday
{
    char *ptrday;
    struct weekday *next;
};

static void matchtest(char *eday, struct weekday *head)
{
    struct weekday *cursor = head;

    do
    {
        if (strcmp(eday, cursor->ptrday) == 0)
        {
            printf("The next day of the week after %s is %s\n", eday, cursor->next->ptrday);
            return;
        }
        cursor = cursor->next;
    } while (cursor != head);

    printf("The 'day of the week' %s does not match any day of the week!\n", eday);
}

int main(void)
{
    char enteredday[80];
    struct weekday sunday, monday, tuesday, wednesday, thursday, friday, saturday;
    struct weekday *head = &sunday;

    sunday.ptrday = "Sunday";
    monday.ptrday = "Monday";
    tuesday.ptrday = "Tuesday";
    wednesday.ptrday = "Wednesday";
    thursday.ptrday = "Thursday";
    friday.ptrday = "Friday";
    saturday.ptrday = "Saturday";

    sunday.next = &monday;
    monday.next = &tuesday;
    tuesday.next = &wednesday;
    wednesday.next = &thursday;
    thursday.next = &friday;
    friday.next = &saturday;
    saturday.next = &sunday;

    printf("This is a test for the next day of the week.\n\n");
    printf("Enter a day ");

    if (scanf("%79s", enteredday) == 1)
    {
        printf("Got: [%s]\n", enteredday);
        matchtest(enteredday, head);
    }

    return 0;
}

示例运行

代码语言:javascript
复制
$ for d in Sunday Monday Tuesday Wednesday Thursday Friday Saturday Otherday; do ./nwd <<< $d; done
This is a test for the next day of the week.

Enter a day Got: [Sunday]
The next day of the week after Sunday is Monday
This is a test for the next day of the week.

Enter a day Got: [Monday]
The next day of the week after Monday is Tuesday
This is a test for the next day of the week.

Enter a day Got: [Tuesday]
The next day of the week after Tuesday is Wednesday
This is a test for the next day of the week.

Enter a day Got: [Wednesday]
The next day of the week after Wednesday is Thursday
This is a test for the next day of the week.

Enter a day Got: [Thursday]
The next day of the week after Thursday is Friday
This is a test for the next day of the week.

Enter a day Got: [Friday]
The next day of the week after Friday is Saturday
This is a test for the next day of the week.

Enter a day Got: [Saturday]
The next day of the week after Saturday is Sunday
This is a test for the next day of the week.

Enter a day Got: [Otherday]
The 'day of the week' Otherday does not match any day of the week!
$

如您所知,我不喜欢多次键入输入。

使用typedef编写代码

还可以在数组中使用链表(尽管您也可以只使用日期名称的数组)。

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>

typedef struct Weekday Weekday;
struct Weekday
{
    char *ptrday;
    Weekday *next;
};

Weekday days_of_the_week[7] =
{
    { "Sunday",    &days_of_the_week[1] },
    { "Monday",    &days_of_the_week[2] },
    { "Tuesday",   &days_of_the_week[3] },
    { "Wednesday", &days_of_the_week[4] },
    { "Thursday",  &days_of_the_week[5] },
    { "Friday",    &days_of_the_week[6] },
    { "Saturday",  &days_of_the_week[0] },
};

static void matchtest(char *eday, Weekday *head)
{
    Weekday *cursor = head;

    do
    {
        if (strcmp(eday, cursor->ptrday) == 0)
        {
            printf("The next day of the week after %s is %s\n", eday, cursor->next->ptrday);
            return;
        }
        cursor = cursor->next;
    } while (cursor != head);

    printf("The 'day of the week' %s does not match any day of the week!\n", eday);
}

int main(void)
{
    char enteredday[80];

    printf("This is a test for the next day of the week.\n\n");
    printf("Enter a day ");

    if (scanf("%79s", enteredday) == 1)
    {
        printf("Got: [%s]\n", enteredday);
        matchtest(enteredday, days_of_the_week);
    }

    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20603872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档