您的位置:首页 > 其它

线性表顺序存储的各项操作的实现

2015-07-30 10:12 429 查看
该文中主要采用顺序存储来完成对线性表的各项操作,通过动态分配的一维数组来实现,主要包括初始化、插入、删除、取第i个数据元素、定位、销毁、合并两个有序表以及在有序表中添加元素依旧保持其有序。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define lISTINCREMENT 10


typedef int ElemType;
typedef int Status;

typedef struct SqList
{
ElemType *elem;//存储空间基址
int length;//当前长度
int listsize;//当前的存储容量
}SqList;

Status InitList(SqList *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;
}

Status DestroyList(SqList *L)//销毁表
{
if(L->elem!=NULL)
{
free(L->elem);
L->elem=NULL;
L->length=0;
L->listsize=0;
}
printf("Destroy List\n");
return OK;

}

Status ListInsert(SqList *L,int i, ElemType e)//插入
{
if(i<1||i>L->length+1)
return ERROR;
if(L->length >= L->listsize)
{
ElemType *newbase = (ElemType *)realloc(L->elem,(L->listsize+lISTINCREMENT)*sizeof(ElemType));
if(!newbase)
exit(OVERFLOW);
L->elem=newbase;
L->listsize+=lISTINCREMENT;
}
ElemType *q =&(L->elem[i-1]);
ElemType *p;
for (p=&(L->elem[L->length-1]);p>=q;--p)
{
*(p+1)=*p;
}
*q=e;
++L->length;
return OK;
}
void IcreaseInsert(SqList *L, ElemType e)//L有序递增,将e插入依旧保持递增
{
int i;
for(i=0;i<=L->length;i++)
{
if(e<=L->elem[0])
{
ListInsert(L,1,e);
break;
}
else if(e>=L->elem[i]&&e<=L->elem[i+1])
{
ListInsert(L,i+2,e);
break;
}
}

}
Status ListDelete(SqList *L,int i, ElemType e)//删除
{
if(i<1||i>L->length+1)
return ERROR;
ElemType *p = &(L->elem[i-1]);
e=*p;
ElemType *q =L->elem+L->length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L->length;
return OK;
}

int GetElement(SqList *L,int i)//获取第i个位置上的元素
{
if(i<1||i>L->length+1)
return ERROR;
return L->elem[i-1];
}

int LocateElem(SqList *L, ElemType e)//定位元素e
{
int i=0;
while(i<=L->length&&L->elem[i]!=e)
++i;
if (i<=L->length)
return i+1;
else
return -1;

}

void UnionList(SqList *La,SqList *Lb, SqList *L)//合并两个有序表
{
int i=0,j=0;
int k;
for(k=0;k<(La->length+Lb->length);k++)
{
if(La->elem[i]<=Lb->elem[j])
{
L->elem[k]=La->elem[i];
i++;
}
else if(La->elem[i]>Lb->elem[j])
{
L->elem[k]=Lb->elem[j];
j++;
}
}
L->length=k;
}
void PrintList(SqList *L)//打印
{
int i;
for(i = 0; i<L->length;i++)
{
printf("%4d",L->elem[i]);
}

printf("\n");
}

int main()
{
SqList *L;
SqList *P,*Q;
ElemType e;
int k,i;
int quit=1;
L=(SqList *)malloc(sizeof(SqList));
InitList(L);
P=(SqList *)malloc(sizeof(SqList));
InitList(P);
Q=(SqList *)malloc(sizeof(SqList));
InitList(Q);
for(i=1;i<=5;i++)
ListInsert(L,i,i*2);
for(i=1;i<=5;i++)
ListInsert(P,i,i*3);
PrintList(L);
while(quit==1)
{
printf("1.Insert 2.Delete 3.getElement 4.Locate   5.IncreaseInsert 6.Union 7.Destroy\n");
scanf("%d",&k);
getchar();
switch(k)
{
case 1:
PrintList(L);
printf("insert location and value\n");
scanf("%d %d",&i,&e);
ListInsert(L,i,e);
PrintList(L);
break;

case 2:
PrintList(L);
printf("insert location you want delete\n");
scanf("%d",&i);
ListDelete(L,i,e);
PrintList(L);
break;

case 3:
PrintList(L);
printf("insert location you want get\n");
scanf("%d",&i);
printf("%d\n",GetElement(L,i));InitList(P);
break;

case 4:
PrintList(L);
printf("insert value you want locate\n");
scanf("%d",&e);
printf("%d\n",LocateElem(L,e));
break;
case 5:
PrintList(L);
printf("insert value you want increase insert\n");
scanf("%d",&e);
IcreaseInsert(L,e);
PrintList(L);
break;
case 6:
PrintList(L);
PrintList(P);
UnionList(L,P,Q);
PrintList(Q);
break;
case 7:
DestroyList(L);
break;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息