|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
用单链表实现如下图的结果:
这是代码:
- #include <stdio.h>
- #include <stdlib.h>
-
- struct Date
- {
- int year;
- int month;
- int day;
- };
-
- struct Record
- {
- char name[16];
- int age;
- struct Date first;
- struct Date second;
- struct Record *next;
- };
- void getInput(struct Record *record);
- void printRecord(struct Record *head);
- void addRecord(struct Record **head);
- void releaseRecord(struct Record *head);
-
- void getInput(struct Record *record)
- {
- printf("请问姓名是:");
- scanf("%s", record->name);
- printf("请问年龄是:");
- scanf("%d", &record->age);
- printf("请问是否接种过疫苗(Y/N):");
-
- getchar();
- if (getchar() != 'Y')
- {
- record->first.year = 0;
- printf("请尽快接种疫苗!\n");
- }
- else
- {
- printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
- scanf("%d-%d-%d", &record->first.year, &record->first.month, &record->first.day);
-
- printf("请问是否接种第二针疫苗(Y/N):");
- getchar();
- if (getchar() != 'Y')
- {
- record->second.year = 0;
- printf("请尽快接种第二针疫苗!\n");
- }
- else
- {
- printf("请输入第二针疫苗接种的日期(yyyy-mm-dd):");
- scanf("%d-%d-%d", &record->second.year, &record->second.month, &record->second.day);
- }
- }
- putchar('\n');
- }
-
- void printRecord(struct Record *head)
- {
- struct Record *record;
-
- record = head;
-
- while (record != NULL)
- {
- printf("姓名:%s,年龄:%d\n", (*record).name, (*record).age);
-
- if (record->first.year == 0)
- {
- printf("未接种疫苗!\n\n");
- }
- else
- {
- printf("第一针疫苗接种日期:%d-%d-%d,", (*record).first.year, (*record).first.month, (*record).first.day);
- }
-
- if ((*record).first.year != 0 && (*record).second.year == 0)
- {
- printf("未接种第二针疫苗!\n\n");
- }
- else if((*record).first.year != 0)
- {
- printf("第二针疫苗接种日期:%d-%d-%d\n\n", (*record).second.year, (*record).second.month, (*record).second.day);
- }
-
- record = record -> next;
- }
- }
-
- void addRecord(struct Record **head)
- {
- struct Record *record, *temp;
-
- record = (struct Record *)malloc(sizeof(struct Record));
- if (record == NULL)
- {
- printf("内存分配失败!\n");
- exit(1);
- }
-
- getInput(record);
-
- if (*head != NULL)
- {
- temp = *head;
- *head = record;
- record->next = temp;
- }
- else
- {
- *head = record;
- record->next = NULL;
- }
- }
-
- void releaseRecord(struct Record *head)
- {
- struct Record *temp;
-
- while (head != NULL)
- {
- temp = head;
- head = head->next;
- free(temp);
- }
- }
-
- int main(void)
- {
- struct Record *head = NULL;
- int ch;
-
- while (1)
- {
- printf("请问是否需要录入(Y/N):");
- do
- {
- ch = getchar();
- } while (ch != 'Y' && ch != 'N');
-
- if (ch == 'Y')
- {
- addRecord(&head);
- }
- else
- {
- break;
- }
- }
-
- printf("请问是否需要打印已录入数据(Y/N):");
- do
- {
- ch = getchar();
- } while (ch != 'Y' && ch != 'N');
-
- if (ch == 'Y')
- {
- printRecord(head);
- }
-
- releaseRecord(head);
-
- return 0;
- }
复制代码
在第59行的printRecord函数中,用了局部变量结构体指针*record,并令record = head;
我想问一下,这个局部变量结构体指针*record能省去吗?直接用head指针不好吗?
就是把printRecord函数代码写成这样:
- void printRecord(struct Record *head)
- {
- while(head!=NULL)
- {
- printf("姓名:%s,年龄:%d\n",head->name,head->age);
- if(head->first.year==0)
- {
- printf("未接种疫苗!\n");
- }
- else
- {
- printf("第一针疫苗接种日期:%d-%d-%d,",head->first.year,head->first.month,head->first.day);
- if(head->second.year==0)
- {
- printf("未接种第二针疫苗!\n");
- }
- else
- {
- printf("第二针疫苗接种日期:%d-%d-%d\n",head->second.year,head->second.month,head->second.day);
- }
- }
- putchar('\n');
-
- head=head->next;
- }
- }
复制代码
我试了试,这样程序也可以正常运行,那为什么要多设置一个指针*record呢?
record 是临时指针,这样做的好处是防止改变原有的指针指向。要不要用你看情况而定。
|
|