集合La Lb 链表实现 得到 La U Lb
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
#define FORMANT() \
printf("==================================\n");\
printf("num eng math avg(auto)\n");\
printf("==================================\n")
struct student
{
int num;
float english;
float math;
float avg;
struct student *next;
};
int n= 0;
struct student *creat();
void prt(struct student *head);
struct student *add(struct student *head_La, struct student *head_Lb);
struct student *compare(struct student *head_La, struct student *p_Lb);
int main(int argc, char *argv[])
{
struct student *head_La, *head_Lb, *head_Lc;
int pos,position;
printf("创建链表La:\n");
head_La = creat();
prt(head_La);
n = 0;
printf("\n创建链表Lb:\n");
head_Lb = creat();
prt(head_Lb);
n = 0;
printf("\n生成链表Lc = La U Lb :");
head_Lc = add(head_La, head_Lb);
prt(head_Lc);
system("PAUSE");
return 0;
}
struct student *creat()
{
struct student *p1, *p2, *head = NULL;
p1 = p2 = (struct student *)malloc(LEN);
printf("请输入学生的信息:\n");
FORMANT();
scanf("%d%f%f", &p1->num,&p1->english,&p1->math);
p1->avg = (p1->english + p1->math)/2;
while(p1->num != 0)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%d%f%f", &p1->num,&p1->english,&p1->math);
p1->avg = (p1->english + p1->math)/2;
}
p1->next = NULL;
return head;
}
void prt(struct student *head)
{
struct student *p1;
p1 = head;
printf("\n显示链表:\nnow there are %d datas.\n",n);
FORMANT();
while(p1 != NULL)
{
printf("%-8d%-8.2f%-9.2f%-8.2f\n", p1->num, p1->english, p1->math, p1->avg);
p1=p1->next;
}
}
struct student *add(struct student *head_La, struct student *head_Lb)
{
struct student *head_Lc, *p_La, *p_Lb, *p_Lb1, *p_Lc, *p_Lc1;
p_La = head_La;
p_Lb = head_Lb;
p_Lc = p_Lc1 = (struct student *)malloc(LEN);
head_Lc = p_Lc;
while(p_La != NULL)
{
p_Lc->num = p_La->num;
p_Lc->english = p_La->english;
p_Lc->math = p_La->math;
p_Lc->avg = p_La->avg;
p_La = p_La->next;
p_Lc1->next = p_Lc;
p_Lc1 = p_Lc;
p_Lc = (struct student *)malloc(LEN);
n++;
}
while(p_Lb != NULL)
{
p_Lb1 = compare(head_La, p_Lb);
if(p_Lb1 != NULL)
{
p_Lc->num = p_Lb1->num;
p_Lc->english = p_Lb1->english;
p_Lc->math = p_Lb1->math;
p_Lc->avg = p_Lb1->avg;
p_Lc1->next = p_Lc;
p_Lc1 = p_Lc;
p_Lc = (struct student *)malloc(LEN);
n++;
}
p_Lb = p_Lb->next;
}
return head_Lc;
}
struct student *compare(struct student *head_La, struct student *p_Lb)
{
struct student *p_La = head_La;
int flag = 0;
while(p_La != NULL)
{
if(p_La->num != p_Lb->num)
flag = 1;
else
flag = 0;
p_La = p_La->next;
}
if(flag)
return p_Lb;
else
return NULL;
}
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
#define FORMANT() \
printf("==================================\n");\
printf("num eng math avg(auto)\n");\
printf("==================================\n")
struct student
{
int num;
float english;
float math;
float avg;
struct student *next;
};
int n= 0;
struct student *creat();
void prt(struct student *head);
struct student *add(struct student *head_La, struct student *head_Lb);
struct student *compare(struct student *head_La, struct student *p_Lb);
int main(int argc, char *argv[])
{
struct student *head_La, *head_Lb, *head_Lc;
int pos,position;
printf("创建链表La:\n");
head_La = creat();
prt(head_La);
n = 0;
printf("\n创建链表Lb:\n");
head_Lb = creat();
prt(head_Lb);
n = 0;
printf("\n生成链表Lc = La U Lb :");
head_Lc = add(head_La, head_Lb);
prt(head_Lc);
system("PAUSE");
return 0;
}
struct student *creat()
{
struct student *p1, *p2, *head = NULL;
p1 = p2 = (struct student *)malloc(LEN);
printf("请输入学生的信息:\n");
FORMANT();
scanf("%d%f%f", &p1->num,&p1->english,&p1->math);
p1->avg = (p1->english + p1->math)/2;
while(p1->num != 0)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf("%d%f%f", &p1->num,&p1->english,&p1->math);
p1->avg = (p1->english + p1->math)/2;
}
p1->next = NULL;
return head;
}
void prt(struct student *head)
{
struct student *p1;
p1 = head;
printf("\n显示链表:\nnow there are %d datas.\n",n);
FORMANT();
while(p1 != NULL)
{
printf("%-8d%-8.2f%-9.2f%-8.2f\n", p1->num, p1->english, p1->math, p1->avg);
p1=p1->next;
}
}
struct student *add(struct student *head_La, struct student *head_Lb)
{
struct student *head_Lc, *p_La, *p_Lb, *p_Lb1, *p_Lc, *p_Lc1;
p_La = head_La;
p_Lb = head_Lb;
p_Lc = p_Lc1 = (struct student *)malloc(LEN);
head_Lc = p_Lc;
while(p_La != NULL)
{
p_Lc->num = p_La->num;
p_Lc->english = p_La->english;
p_Lc->math = p_La->math;
p_Lc->avg = p_La->avg;
p_La = p_La->next;
p_Lc1->next = p_Lc;
p_Lc1 = p_Lc;
p_Lc = (struct student *)malloc(LEN);
n++;
}
while(p_Lb != NULL)
{
p_Lb1 = compare(head_La, p_Lb);
if(p_Lb1 != NULL)
{
p_Lc->num = p_Lb1->num;
p_Lc->english = p_Lb1->english;
p_Lc->math = p_Lb1->math;
p_Lc->avg = p_Lb1->avg;
p_Lc1->next = p_Lc;
p_Lc1 = p_Lc;
p_Lc = (struct student *)malloc(LEN);
n++;
}
p_Lb = p_Lb->next;
}
return head_Lc;
}
struct student *compare(struct student *head_La, struct student *p_Lb)
{
struct student *p_La = head_La;
int flag = 0;
while(p_La != NULL)
{
if(p_La->num != p_Lb->num)
flag = 1;
else
flag = 0;
p_La = p_La->next;
}
if(flag)
return p_Lb;
else
return NULL;
}
转载于:https://blog.51cto.com/qq164587043/78006