c 语言单链表
#include <stdio.h>#include <stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Record
{
char name;
int age;
struct Date first;
struct Date second;
struct Reocrd* next;
};
void addRecord(struct Record** record);
void getInput(struct Record* record);
void printRecord(struct Record* record);
void releaseRecord(struct Record* record);
void getInput(struct Record* record)
{
char ch;
printf("请问姓名是:");
scanf("s", record->name);
printf("请问年龄是:");
scanf("%d", record->age);
printf("请问是否接种过疫苗(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
printf("请输入第一针疫苗的接种日期:");
scanf("%d-%d-%d", &record->first.year, &record->first.month, &record->first.day);
printf("请问是否接种过第二针疫苗(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
printf("请输入第二针疫苗的接种日期:");
scanf("%d-%d-%d", &record->second.year, &record->second.month, &record->second.day);
}
else
{
record->second.year = 0;
}
}
else
{
record->first.year = 0;
}
}
void addRecord(struct Record** record)
{
struct Record* temp;
struct Record* pt;
pt = (struct Record*)malloc(sizeof(struct Record));
if (pt == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getInput(pt);
if (*record != NULL)
{
temp = *record;
*record = pt;
pt->next = temp;
}
else
{
*record = pt;
pt->next = NULL;
}
}
void printRecord(struct Record* record)
{
struct Record* pt;
pt = record;
while (pt != NULL)
{
printf("姓名:%s,年龄:%d\n", record->name, record->age);
if (record->first.year == 0)
{
printf("请尽快接种疫苗!\n");
}
else
{
printf("第一针疫苗接种日期:%d-%d-%d,", record->first.year, record->first.month, record->first.day);
if (record->second.year == 0)
{
printf("未接种第二针疫苗!\n");
}
else
{
printf("第二针疫苗的接种日期:%d-%d-%d\n", record->second.year, record->second.month, record->second.day);
}
}
}
}
void releaseRecord(struct Record* record)
{
struct Record* temp;
while (record != NULL)
{
temp = record;
record = record->next;
free(temp);
}
}
int main(void)
{
struct Record* record=NULL;
char ch;
while (1)
{
printf("请问是否需要录入(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
addRecord(&record);
}
else
{
break;
}
}
printRecord(record);
releaseRecord(record);
return 0;
} 本帖最后由 jackz007 于 2022-3-12 16:36 编辑
将此句
#define _CRT_SECURE_NO_WARNINGS
添加为第一行,可以去掉所有的警告
struct Record
{
char name;
int age;
struct Date first;
struct Date second;
struct Record * next;// 这一句有错已经修改
}; jackz007 发表于 2022-3-12 16:24
将此句
添加为第一行,可以去掉所有的警告
感谢感谢! 怪不得,检查了无数次了{:10_266:}
页:
[1]