#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#define OK 1
#define EQUAL 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_STZE 100
#define LISTINCREMENT 10
struct STU{
char name[20];
char stuno[10];
int age;
int score;
}stu[50];
typedef struct STU ElemType;
struct LNode{
ElemType data;
struct LNode *next;
};
typedef struct LNode LNode;
typedef struct LNode *linklist;
int init(linklist *L)
{
*L=(LNode *)malloc(sizeof(LNode));
if(!L) exit(ERROR);
(*L)->next=NULL;
return OK;
}
int listlength(linklist L)
{
int j=0;
while(L->next)
j++;
return j;
}
int GetElem(linklist L,int i, ElemType *e)
{
linklist p;int j;
p=L->next; j=1;
while(p&&j<i){
p=L->next; ++j;
}
if (!p||j>1)
return ERROR;
*e=p->data;
return OK;
}
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(linklist la,ElemType e,int type)
{
int i;
linklist p;
p=la;
switch(type)
{
case EQUAL:
while(p->next)
{
p=p->next;
if(EqualList(&p->data,&e))
return 1;
}
break;
dafault: break;
}
return 0;
}
void MergeList (linklist la,linklist lb,linklist *lc)
{
linklist pa, pb, pc;
pa=la->next;
pb=lb->next;
*lc=pc=la;
while(pa&&pb)
{
if(less_equalList(&pa->data,&pb->data))
{
pc->next=pa;pc=pa;pa=pa->next;
}
else
{
pa->next=pb;pc=pb;pb=pb->next;
}
}
pc->next=pa?pa:pb;
free(lb);
}
int printlist(linklist L)
{
int i;
linklist p;
p=L;
printf("name stuno age score\n");
while(p->next)
{
p=p->next;
printf("%s\t%s\t%d\t%d\n",p->data.name,p->data.stuno,p->data.age,p->data.score);
printf("\n");
}
}
int ListInsert(linklist L,int i, ElemType e)
{
linklist p,s;
int j;
p=L;
j=0;
while(p&&j<i-1)
{
p=p->next;
++j;
}
if(!p||j>i-1)
return ERROR;
s=(linklist)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
}
void main()
{
struct STU e;
linklist la,lb,lc;
clrscr();
printf("\n\n..................List Demo is running.........................\n\n");
printf("First is Insertlist function .\n");
init(&la);
strcpy(e.name,"stu1");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(la,1,e);
strcpy(e.name,"stu3");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(la,2,e);
printlist(la);
getch();
printf("Second is Insertlist function .\n");
init(&lb);
strcpy(e.name,"stu5");
strcpy(e.stuno,"100003");
e.age=80;
e.score=1000;
ListInsert(lb,1,e);
strcpy(e.name,"stu6");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(lb,2,e);
printlist(lb);
getch();
MergeList(la,lb,&lc);
printlist(lc);
getch();
}
实验二:线性表的链式存储结构(验证性实验)
最新推荐文章于 2022-03-25 15:12:02 发布