(数据结构)验证表:
应用中有时需要验证来自不同地方的两个表的信息是否一致。本实验编写具有如下功能的程序:输入两个学生记录表LIST1,LIST2,在表LIST2中找出所有没有在表LIST1中出现的学生记录(设表LIST1为基础数据表,非空)。
每一个学生记录元素包含两个数据项:学号(整数),姓名;
如果学生记录表LIST2中的记录都包含在LIST1中,则输出the records of LIST2 are all in the LIST1.
如果学生记录表LIST2中的存在学号,姓名不能与表LIST1完全匹配的记录,则输出 学号(%8d)姓名(%15s)is not in LIST1.
如果LIST2为空表,则输出the LIST2 is NULL.
| 测试输入 | 期待输出 |
|---|---|
| 5↵ 20120001 zhangli↵ 20120002 wanglei↵ 20120003 duyang↵ 20120004 lixin↵ 20120005 liufan↵ 3↵ 20120001 zhangli↵ 20120006 lixin↵ 20120002 wanglei↵ | 20120006 lixin is not in LIST1.↵ |
| 5↵ 20120001 zhangli↵ 20120002 wanglei↵ 20120003 duyang↵ 20120004 lixin↵ 20120005 liufan↵ 3↵ 20120001 zhangli↵ 20120004 wanglei↵ 20120006 duyang↵ | 20120004 wanglei is not in LIST1.↵ 20120006 duyang is not in LIST1.↵ |
| 5↵ 20120001 zhangli↵ 20120002 wanglei↵ 20120003 duyang↵ 20120004 lixin↵ 20120005 liufan↵ 0↵ | the LIST2 is NULL.↵ |
| 5↵ 20120001 zhangli↵ 20120002 wanglei↵ 20120003 duyang↵ 20120004 lixin↵ 20120005 liufan↵ 3↵ 20120002 wanglei↵ 20120001 zhangli↵ 20120004 lixin↵ | the records of LIST2 are all in the LIST1.↵ |
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct L
{
long id;
char name[15];
struct L *next;
} NODE;
int main()
{
NODE *head, *p, *q;
int n;
int flag = 0, total = 0;
scanf("%d", &n);
head=(NODE *)malloc(sizeof(NODE));
head->next = NULL;
q = head;
for(int i = 0; i < n; i++)
{
p = (NODE *)malloc(sizeof(NODE));
for(int j = 0; j < 15; j++)
(*p).name[j] = '\0';
scanf("%ld %s", &p->id, &p->name);
p->next = NULL;
q->next = p;
q = q->next;
}
scanf("%d", &n);
if(n==0) printf("the LIST2 is NULL.\n");
else
{
for(int i = 0; i < n; i++)
{
flag = 0;
p = (NODE *)malloc(sizeof(NODE));
for(int j = 0; j < 15; j++)
(*p).name[j] = '\0';
scanf("%ld %s", &p->id, &p->name);
q = head;
do
{
q = q->next;
if(q->id == p->id && strcmp(p->name, q->name) == 0)
{
flag = 1;
total++;
break;
}
}while(q->next != NULL);
if(flag == 0) printf("%8d %s is not in LIST1.\n",p->id,p->name);
}
if(total == n) printf("the records of LIST2 are all in the LIST1.\n");
}
}
学生记录表验证算法
9229

被折叠的 条评论
为什么被折叠?



