您的位置:首页 > 编程语言 > C语言/C++

线性表的顺序表示(C语言实现)

2011-05-03 12:52 651 查看
#include<stdio.h>
#include<malloc.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;

/*构造一个空的线性表L*/

int init(List *L)
{
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem) exit(OVERFLOW);
L->Length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}/*init */

/*返回L中数据元素的个数*/

int ListLength(List *L)
{
return L->Length;
}

/*若线性表L存在,1<=i<=L.Length,用e返回L中第i个元素*/
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)
return 1;
else
return 0;
}

/*判断元素的大小,通过元素的name字段*/

int Less_EqualList(ElemType *e1,ElemType *e2)
{
if (strcmp(e1->name,e2->name)<=0)
return 1;
else
return 0;
}

/*若cur_e是L的元素,且不是第一个,则用pre_e返回它的前驱*/

int PriorElem(List *La,ElemType cur_e,ElemType *pre_e)
{
int i;
i=LocateElem(La,cur_e,EQUAL);
{
if (i>=1)
{
*pre_e=La->elem[i-1]; /*»òдΪLa->elem[i-1]*/
return OK;
}
else
{
return ERROR;
}
}
}

/*若cur_e是L的元素,且不是最后一个,则用next_e返回它的后继元素*/

int NextElem(List *La,ElemType cur_e,ElemType *next_e)
{
int i;
if (LocateElem(La,cur_e,i))
{
if (i<=La->Length-1)
{
*next_e=(*La).elem[i+1];
return OK;
}
else
{
return ERROR;
}
}
}

/*返回线性表L中第一个与e满足关系type的位序*/

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 i;
break;
default:
break;
}
return 0;
}

/*从线性表L中删除第i个元素,并且删除的元素赋给e*/

int DeleteElem(List *L,int i,ElemType e)
{
ElemType *p,*q;
if ((i<1) || (i>L->Length)) return ERROR;
p=&L->elem[i-1];
e=*p;
q=L->elem+L->Length-1; /* Also Write: q=&L->elem[0]+L->Length-1;*/
for (++p;p<=q;++p)
*(p-1)=*p;
--L->Length;
return OK;
}

/*清空线性表L*/

void ClearList(List *L)
{
L->Length=0;
}

/*销毁线性表L*/

int DestroyList(List *L)
{
int i;
for (i=0;i<=L->Length-1;i++)
free(&L->elem[i]);
L->Length=0;
}

/*合并线性表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)
{
if(Less_EqualList(pa,pb)) *pc++=*pa++;
else *pc++=*pb++;
}
while(pa<=pa_last) *pc++=*pa++;
while(pb<=pb_last) *pc++=*pb++;
}

/*将所有在线性表Lb中但不早La中的数据元素插入到La中*/

void UnionList(List *La, List *Lb)
{
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);
}

}

/*打印线性表L*/

int printlist(List L)
{
int i;
printf("name stuno age score\n");
for(i=0;i<L.Length;i++)
printf("%-10s %s\t%d\t%d\n", L.elem[i].name, L.elem[i].stuno,
L.elem[i].age, L.elem[i].score);
printf("\n");
}

/*将元素e插入线性表L的第i个元素中*/

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,t,*k;
List La,Lb,Lc,*L;
int i;

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);
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);

printlist(La);
printf("List A length now is %d.\n\n",La.Length);
getch();

strcpy(t.name,"stu5");
strcpy(t.stuno,"100003");
t.age=80;
t.score=1000;
L=&La;
i=LocateElem(L,t,EQUAL);
printf("The position of t is %d\n",i);

PriorElem(L,t,k);
printf("The prior name is %s\n",k->stuno);
getch();

DeleteElem(&La,2,e);
printlist(La);
getch();

DestroyList(&La);
printlist(La);
getch();
init(&Lb);
strcpy(e.name,"stu2");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(&Lb,1,e);
strcpy(e.name,"stu4");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
ListInsert(&Lb,2,e);

strcpy(e.name,"stu6");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
ListInsert(&Lb,3,e);

printlist(Lb);
printf("List B length now is %d.\n\n",Lb.Length);
getch();

MergeList(&La,&Lb,&Lc);
printlist(Lc);
getch();

printf("Second is UnionList function.\n");
printf("Now union List A and List B.....\n");
UnionList(&La,&Lb);
printlist(La);
printf("List A length now is %d.\n\n",La.Length);
getch();
printf("\n\n\n\n\n\nWelcome to visit http://zmofun.heha.net !\n\n\n\n\n\n\n");
getch();
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/djcsch2001/archive/2009/03/05/3961065.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: