C链表在使用然后删除typedef语句后不再工作
我有一个程序在这里显示从输入的一天开始的一周的第二天。这不是一个实用或有效的程序,我只是用它来测试我学到的概念。它工作了一段时间,但在了解了typedef语句之后,我尝试在程序的结构“weekday”中使用它们来声明该类型的变量,但随后删除了typedef,因为它产生了错误(例如“不兼容的指针”和“不完整的定义”)。现在,即使我取出typedef语句,该文件仍显示错误。最奇怪的是,如果我将所有代码复制并粘贴到一个新文件中,错误就不会出现。有人知道是什么导致了这些错误吗C链表在使用然后删除typedef语句后不再工作,c,pointers,linked-list,typedef,C,Pointers,Linked List,Typedef,我有一个程序在这里显示从输入的一天开始的一周的第二天。这不是一个实用或有效的程序,我只是用它来测试我学到的概念。它工作了一段时间,但在了解了typedef语句之后,我尝试在程序的结构“weekday”中使用它们来声明该类型的变量,但随后删除了typedef,因为它产生了错误(例如“不兼容的指针”和“不完整的定义”)。现在,即使我取出typedef语句,该文件仍显示错误。最奇怪的是,如果我将所有代码复制并粘贴到一个新文件中,错误就不会出现。有人知道是什么导致了这些错误吗 #include <
#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;
}
#包括
#包括
结构工作日{
char*ptrday;
结构工作日*下一个;
};
无效匹配测试(char*eday,struct weekday*head,struct weekday*游标){
光标=头部;
布尔相等字符串=真;
while(光标!=NULL){
char*p=cursor->ptrday;
equalstring=true;
而(*eday!='\0'){
如果(*eday!=*p){
equalstring=false;
打破
}
++eday;++p;
}
if(equalstring==1){
printf(“一周的第二天是%s\n”,光标->下一天->ptrday);
打破
}
光标=光标->下一步;
}
if(equalstring==false)
printf(“一周的第二天是星期日”);
}
内部主(空){
字符输入日期[80],*ptreday=输入日期;
结构工作日周日、周一、周二、周三、周四、周五、周六;
结构工作日*head=&sunday;
struct weekday*cursor=NULL;
sunday.ptrday=“sunday”周一.ptrday=“周一”周二.ptrday=“周二”;
星期三。ptrday=“星期三”;星期四。ptrday=“星期四”;
星期五。ptrday=“星期五”;星期六。ptrday=“星期六”;
星期日。下一个=&星期一;星期一。下一个=&星期二;星期二。下一个=&星期三;
周三。下一个=&周四;周四。下一个=&周五;周五。下一个=&周六;
星期六。下一个=空;
printf(“这是一周中第二天的测试。\n\n”);
printf(“输入日期”);
scanf(“%s”,输入日期);
匹配测试(ptreday、head、cursor);
返回0;
}
您遇到的一个问题是,您在comparator函数中修改了eday
,但是如果比较失败,您无法将搜索设置为从字符串的开头开始,因为您不知道字符串从何处开始。这折磨着“星期四”,报告说“第二天”是星期天。对于其他一些测试,这是可行的。星期六之后的第二天被报道为星期天,因为混乱
您还有一个问题,周六后的第二天是未定义的
下面是合理的工作代码。我使用strcmp()(因此也使用
)进行比较。我将while(cursor!=NULL)
循环转换为do{…}while(cursor!=head)代码>循环,以便循环链表正常工作。我也会在读取时打印输入,然后再打印正式输出。这允许我使用bash
的符号进行测试(您会遇到什么错误?看起来您需要删除旧的对象文件并重新编译以获得新的文件。故事的一个寓意是“使用VCS”(版本控制系统)。一旦您做出的更改破坏了某些正在工作的内容,就应该确保您在VCS中有工作代码,以便您可以恢复工作代码并将工作代码与损坏的代码进行比较。奇怪的是,星期日不是星期六之后的第二天。您的代码不能正确处理其他日期作为条目(如果给出的名称不是一周中某一天的名称,则不会报告问题)。代码在我的电脑上运行正常。看起来@Dayalrai是正确的。
#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;
}
$ 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!
$
#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;
}