#include<stdio.h>#include<malloc.h>#include<conio.h>/*符号常量*/#define ERROR 0#define OK 1#define EQUAL 1#define OVERFLOW -1#define LIST_INIT_SIZE 100 /* 线性表存储空间的初始分配量 */#define LISTINCREMENT 10 /* 线性表存储空间的分配增量 */struct STU{ /*定义学生结构体类型,包括姓名,学号,年龄,成绩*/ char name[20]; char stuno[10]; int age; int score;}stu[50];typedef struct STU ElemType; /*用ElemType代替学生*/struct LIST{ /*定义表LIST为结构体类型*/ ElemType *elem; /*存储空间基址*/ int length; /*当前长度*/ int listsize; /*当前分配的存储容量*/};typedef struct LIST List; /*用list代表结构体LIST*/int init(List *L){ /*构造一个空的线性表*/ L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if (!L->elem) exit(OVERFLOW); /* 存储分配失败*/ L->length=0; /*空表长度为0*/ L->listsize=LIST_INIT_SIZE; /*初始存储容量*/ return OK;}int Listlength(List *L){ /* 求表L的长度 */ return L->length;}void GetElem(List L, int i, ElemType *e){ *e=L.elem[i];}int EqualList(ElemType *e1,ElemType *e2){ if (strcmp(e1->name,e2->name)==0) /* 以元素e1,e2中的姓名项是否相等作为判定e1,e2是否相等的标准 */ return 1; else return 0;}int Less_EqualList(ElemType *e1,ElemType *e2){ /* 以姓名(字符串)的≤作为判定e1≤e2的标准 */ if (strcmp(e1->name,e2->name)<=0) return 1; else return 0;}int LocateElem(List *La,ElemType e,int type){ /* 判断La中是否有与e符合关系type的元素 */ int i; switch(type) {case EQUAL: for(i=0;i<La->length;i++) if (EqualList(&La->elem[i],&e)) return 1; break;default:break; } return 0;}/* 合并表La,Lb,用Lc存储。已知La,Lb元素值按非递减排列,Lc中值也按非递减排列 */void MergeList(List *La,List *Lb,List *Lc){ ElemType *pa,*pb,*pc,*pa_last,*pb_last; pa=La->elem;pb=Lb->elem; Lc->listsize=Lc->length=La->length+Lb->length; pc=Lc->elem=(ElemType *)malloc(Lc->listsize*sizeof(ElemType)); if (!Lc->elem) exit(OVERFLOW); pa_last=La->elem+La->length-1; pb_last=Lb->elem+Lb->length-1; while(pa<=pa_last&&pb<=pb_last) { /* 合并,Lc元素按非递减排列 */ if (Less_EqualList(pa,pb)) *pc++=*pa++; else *pc++=*pb++; } while (pa<=pa_last) /* 插入La的剩余元素 */ *pc++=*pa++; while (pb<=pb_last) /* 插入Lb的剩余元素 */ *pc++=*pb++;}void UnionList(List *La ,List *Lb){ /* 将所有在Lb中而不在La中的元素插入到La中 */ int La_len,Lb_len; int i; ElemType e; La_len=Listlength(La);Lb_len=Listlength(Lb); /* 求线性表长度 */ for(i=0;i<Lb_len;i++) { GetElem(*Lb,i,&e); if (!LocateElem(La,e,EQUAL)) ListInsert(La,++La_len,e); }}int printlist(List L) /* 输入表L */{ int i; printf("name stuno age score\n"); for (i=0;i<L.length;i++)printf("%s\t%s\t%d\t%d\n",L.elem[i].name,L.elem[i].stuno,L.elem[i].age,L.elem[i].score); printf("\n");}int ListInsert(List *L,int i,struct STU e){ /* 在表L中第i位上插入e */ struct STU *p,*q; if (i<1||i>L->length+1) return ERROR; /* i值不合法 */ q=&(L->elem[i-1]); for(p=&L->elem[L->length-1];p>=q;--p) *(p+1)=*p; *q=e; ++L->length; return OK;}void main(){ struct STU e; /* 定义结构体变量e */ List La,Lb,Lc; /* 定义结构体变量,即表La,Lb,Lc */ printf("\n\n--------List Demo is running ----------\n\n"); printf("First is InsertList function.\n"); init(&La); /* 创建一个新表La */ strcpy(e.name, "stu1"); strcpy(e.stuno, "100001"); e.age=80; e.score=1000; ListInsert(&La,1,e); /* 在La的第1位上插入stu1的数据元素 */ strcpy(e.name, "stu3"); strcpy(e.stuno, "100002"); e.age=80; e.score=1000; ListInsert(&La,2,e); /* 在La的第2位上插入stu3的数据元素 */ printlist(La); /* 输出La */ printf("List A length now is %d.\n\n",La.length); getch(); strcpy(e.name, "stu5"); strcpy(e.stuno, "100003"); e.age=80; e.score=1000; ListInsert(&La,3,e); /* 在表La的第3位上插入stu5的数据表 */ printlist(La); /* 输出表La */ printf("List A length now is %d.\n\n",La.length); getch(); init(&Lb); /* 创建一张新表Lb */ strcpy(e.name, "stu2"); strcpy(e.stuno, "100001"); e.age=80; e.score=1000; ListInsert(&Lb,1,e); /* 在表Lb的第1位上插入stu2的数据 */ strcpy(e.name, "stu4"); strcpy(e.stuno, "100002"); e.age=80; e.score=1000; ListInsert(&Lb,2,e); /* 在表Lb的第2位上插入stu4的数据 */ strcpy(e.name, "stu6"); strcpy(e.stuno, "100001"); e.age=80; e.score=1000; ListInsert(&Lb,3,e); /* 在表Lb的第3位上插入stu6的数据 */ printlist(Lb); /* 输出表Lb */ printf("List B length now is %d.\n\n",Lb.length); getch(); MergeList(&La,&Lb,&Lc); /* 合并表La,Lb,用表Lc存储(非递减有序) */ printlist(Lc); /* 输出表Lc */ getch(); printf("Second is UnionList function.\n "); printf("Now Union List A and List B---\n "); UnionList(&La,&Lb); /* 合并La,Lb,并删除值相同的元素,用La存储 */ printlist(La); /* 输出La */ printf("List A length now is %d.\n\n ",La.length); getch();}