#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;
struct LIST
{
ElemType *elem;
int length; //实际长度
int listsize; //容量
} ;
typedef struct LIST List;
int init(List * L)
{
//初始化,分配100个结构体长度的空间
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem) exit(OVERFLOW);//分配失败退出
L->length=0;//初始长度为-
L->listsize=LIST_INIT_SIZE;//容量为100
return OK;
} /*init */
int ListLength(List * L)
{
return L->length;//返回L.实际长度
}
void GetElem(List L, int i,ElemType * e)
{
*e=L.elem[i];//返回L.元素i
}
int EqualList(ElemType * e1,ElemType * e2)
{
if (strcmp(e1->name,e2->name)==0)//名字相等则认为是相等的
return 1;
else
return 0;
}
int Less_EqualList(ElemType * e1,ElemType * e2)
{
if (strcmp(e1->name,e2->name)<=0)//比较名字,如果小于则返回真
return 1;
else
return 0;
}
int LocateElem(List * La,ElemType e, int 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;
}
void MergeList(List * La,List * Lb,List * Lc)
{
/************************************************************************
void MergeList(List *La,List *Lb,List *Lc)
功能:合并列表
参数:*La 列表类型,a 列表 ->输入
参数:*Lb 列表类型,b 列表 ->输入
参数:*Lc 列表类型,c 列表 ->输出
************************************************************************/
ElemType *pa,*pb,*pc,*pa_last,*pb_last; // 第1,第2,结果,第一最后,第二最后
pa=La->elem;//使pa 指向 列表a的第一个元素
pb=Lb->elem;//使pb 指向 列表b的第二个元素
Lc->listsize = Lc->length = La->length + Lb->length;//列表c的长度和尺寸为列表b的长度+列表a的长度
pc = Lc->elem = (ElemType *)malloc(Lc->listsize * sizeof(ElemType));//给列表c分配存储空间并使pc指向列表c的第一个元素
if(!Lc->elem) //分配失败
exit(OVERFLOW);
pa_last = La->elem + La->length - 1;//使pa_last指向列表a的最后一个元素
pb_last = Lb->elem + Lb->length - 1;//使pb_last指向列表b的最后一个元素
//合并算法的精华所在.
// 1:pa<=pa_last && pb<=pb_last(只要pb>pb_last 或 pa>pa_last 便终止循环)
// 2:if(Less_EqualList(pa,pb))...else...
while(pa<=pa_last && pb<=pb_last) //从第1个开始循环到最后一个
{
if(Less_EqualList(pa,pb))
*pc++=*pa++;
else
*pc++=*pb++;
}
//剩余的全部加上
while(pa<=pa_last)
*pc++=*pa++;
while(pb<=pb_last)
*pc++=*pb++;
}
int UnionList(List * La,List * Lb)
{
int i,j,iTmp;
ElemType *a,*b;
int iExsit ,iResult;
if (Lb->length<=0) return iResult;
for (i=1;i<=Lb->length;i++) {
iExsit=0;
b=Lb->elem+i-1;
for (j=1;j<=La->length;j++) {
a=La->elem+j-1;
if (strcmp(a->name,b->name)==0) {
iExsit=1;
break;
}
}
if (!iExsit) {
iTmp=La->length;
ListInsert(La,++iTmp ,*b);
iResult++;
}
}
return iResult;
}
int printlist(List L)
{
int i;
printf("name stuno age score ");
for(i=0;i<L.length;i++)
printf("%-10s %s %d %d ", L.elem[i].name, L.elem[i].stuno,
L.elem[i].age, L.elem[i].score);
//printf(" ");
return 0;
}
int ListInsert(List * L, int i, struct STU e)
{
struct STU *p,*q;
if (i<1||i>L->length+1)
return ERROR;
q=&(L->elem[i-1]);
for(p=&L->elem[L->length-1];p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return OK;
} /*ListInsert Before i */
main()
{
struct STU e;
List La,Lb,Lc;
//clrscr();
printf(" -------------------线性表 演示正在运行...---------------- 初始化 列表a... ");
init(&La);
printf("第一个是插入演示. ");
strcpy(e.name,"学生1");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(&La,1,e);
strcpy(e.name,"学生3");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(&La,2,e);
printlist(La);
printf("列表a 现在的长度为: %d. ",La.length);
getch();
strcpy(e.name,"学生5");
strcpy(e.stuno,"100003");
e.age=80;
e.score=1000;
ListInsert(&La,3,e);
printlist(La);
printf("现在的长度 %d. ",La.length);
getch();
printf("初始化列表b... ");
init(&Lb);
strcpy(e.name,"学生2");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(&Lb,1,e);
strcpy(e.name,"学生4");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(&Lb,2,e);
strcpy(e.name,"学生6");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(&Lb,3,e);
printlist(Lb);
printf("列表b 现在的长度为: %d. ",Lb.length);
getch();
printf("第二个演示是合并列表演示. ");
printf("现在合并列表a和b... ");
MergeList(&La,&Lb,&Lc);
printlist(Lc);
getch();
printf(" 第三个演示是联合演示. ");
printf("现在联合列表a和b..... ");
UnionList(&La,&Lb);
printlist(La);
printf("列表a的长度为: %d. ",La.length);
getch();
printf(" 欢迎大家一起交流 QQ群: 29940046 ");
getch();
}
#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;
struct LIST
{
ElemType *elem;
int length; //实际长度
int listsize; //容量
} ;
typedef struct LIST List;
int init(List * L)
{
//初始化,分配100个结构体长度的空间
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem) exit(OVERFLOW);//分配失败退出
L->length=0;//初始长度为-
L->listsize=LIST_INIT_SIZE;//容量为100
return OK;
} /*init */
int ListLength(List * L)
{
return L->length;//返回L.实际长度
}
void GetElem(List L, int i,ElemType * e)
{
*e=L.elem[i];//返回L.元素i
}
int EqualList(ElemType * e1,ElemType * e2)
{
if (strcmp(e1->name,e2->name)==0)//名字相等则认为是相等的
return 1;
else
return 0;
}
int Less_EqualList(ElemType * e1,ElemType * e2)
{
if (strcmp(e1->name,e2->name)<=0)//比较名字,如果小于则返回真
return 1;
else
return 0;
}
int LocateElem(List * La,ElemType e, int 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;
}
void MergeList(List * La,List * Lb,List * Lc)
{
/************************************************************************
void MergeList(List *La,List *Lb,List *Lc)
功能:合并列表
参数:*La 列表类型,a 列表 ->输入
参数:*Lb 列表类型,b 列表 ->输入
参数:*Lc 列表类型,c 列表 ->输出
************************************************************************/
ElemType *pa,*pb,*pc,*pa_last,*pb_last; // 第1,第2,结果,第一最后,第二最后
pa=La->elem;//使pa 指向 列表a的第一个元素
pb=Lb->elem;//使pb 指向 列表b的第二个元素
Lc->listsize = Lc->length = La->length + Lb->length;//列表c的长度和尺寸为列表b的长度+列表a的长度
pc = Lc->elem = (ElemType *)malloc(Lc->listsize * sizeof(ElemType));//给列表c分配存储空间并使pc指向列表c的第一个元素
if(!Lc->elem) //分配失败
exit(OVERFLOW);
pa_last = La->elem + La->length - 1;//使pa_last指向列表a的最后一个元素
pb_last = Lb->elem + Lb->length - 1;//使pb_last指向列表b的最后一个元素
//合并算法的精华所在.
// 1:pa<=pa_last && pb<=pb_last(只要pb>pb_last 或 pa>pa_last 便终止循环)
// 2:if(Less_EqualList(pa,pb))...else...
while(pa<=pa_last && pb<=pb_last) //从第1个开始循环到最后一个
{
if(Less_EqualList(pa,pb))
*pc++=*pa++;
else
*pc++=*pb++;
}
//剩余的全部加上
while(pa<=pa_last)
*pc++=*pa++;
while(pb<=pb_last)
*pc++=*pb++;
}
int UnionList(List * La,List * Lb)
{
int i,j,iTmp;
ElemType *a,*b;
int iExsit ,iResult;
if (Lb->length<=0) return iResult;
for (i=1;i<=Lb->length;i++) {
iExsit=0;
b=Lb->elem+i-1;
for (j=1;j<=La->length;j++) {
a=La->elem+j-1;
if (strcmp(a->name,b->name)==0) {
iExsit=1;
break;
}
}
if (!iExsit) {
iTmp=La->length;
ListInsert(La,++iTmp ,*b);
iResult++;
}
}
return iResult;
}
int printlist(List L)
{
int i;
printf("name stuno age score ");
for(i=0;i<L.length;i++)
printf("%-10s %s %d %d ", L.elem[i].name, L.elem[i].stuno,
L.elem[i].age, L.elem[i].score);
//printf(" ");
return 0;
}
int ListInsert(List * L, int i, struct STU e)
{
struct STU *p,*q;
if (i<1||i>L->length+1)
return ERROR;
q=&(L->elem[i-1]);
for(p=&L->elem[L->length-1];p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return OK;
} /*ListInsert Before i */
main()
{
struct STU e;
List La,Lb,Lc;
//clrscr();
printf(" -------------------线性表 演示正在运行...---------------- 初始化 列表a... ");
init(&La);
printf("第一个是插入演示. ");
strcpy(e.name,"学生1");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(&La,1,e);
strcpy(e.name,"学生3");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(&La,2,e);
printlist(La);
printf("列表a 现在的长度为: %d. ",La.length);
getch();
strcpy(e.name,"学生5");
strcpy(e.stuno,"100003");
e.age=80;
e.score=1000;
ListInsert(&La,3,e);
printlist(La);
printf("现在的长度 %d. ",La.length);
getch();
printf("初始化列表b... ");
init(&Lb);
strcpy(e.name,"学生2");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(&Lb,1,e);
strcpy(e.name,"学生4");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(&Lb,2,e);
strcpy(e.name,"学生6");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(&Lb,3,e);
printlist(Lb);
printf("列表b 现在的长度为: %d. ",Lb.length);
getch();
printf("第二个演示是合并列表演示. ");
printf("现在合并列表a和b... ");
MergeList(&La,&Lb,&Lc);
printlist(Lc);
getch();
printf(" 第三个演示是联合演示. ");
printf("现在联合列表a和b..... ");
UnionList(&La,&Lb);
printlist(La);
printf("列表a的长度为: %d. ",La.length);
getch();
printf(" 欢迎大家一起交流 QQ群: 29940046 ");
getch();
}