线性表

#include<stdio.h>
#include<stdlib.h>


typedef int ElemType;

struct List
{
	ElemType *list;
	int size;
	int MaxSize;
};
void againMalloc(struct List *L)
{
	ElemType *p=(ElemType *)realloc(L->list,2*L->MaxSize*sizeof(ElemType));
	if(!p)
	{
		printf("存储空间用完\n");
		exit(1);
	}
	L->list=p;
	L->MaxSize=2*L->MaxSize;
}
void initList(struct List *L,int ms)
{
	if(ms<=0)
	{
		printf("ms值非法\n");
		exit(1);
	}
	L->MaxSize= ms;
	L->list=(ElemType *)malloc(ms*sizeof(ElemType));
	if(!L->size)
	{
		printf("分配内存失败\n");
		exit(1);
	}
	L->size=0;
}
void clearList(struct List *L)
{
	if(L->list!=NULL)
	{
		free(L->list);
		L->list=0;
		L->size=L->MaxSize=0;
	}
}
int sizeList(struct List *L)
{
	return L->size;
}
int emptyList(struct List *L)
{
	if(L->size==0)
	{
		return 1;
	}
	else {
		return 0;
	}
}
ElemType getElem(struct List *L,int pos)
{
	if(pos<1 || pos>L->size)
	{
		printf("元素号越界\n");
		exit(1);
	}
	return L->list[pos-1];
}
void traverseList(struct List *L)
{
	int i;
	for(i=0;i<L->size;i++)
	{
		printf("%d ",L->list[i]);
	}
	printf("\n");
}
int findList(struct List *L,ElemType x)
{
	int i;
	for(i=0;i<L->size;i++)
	{
		if(L->list[i]==x)
		{
			return i;
		}
		else {
			return -1;
		}
	}
}
int updatePosList(struct List *L,int pos,ElemType x)
{
	if(pos<1 || pos>L->size)
	{
		return 0;
	}
	L->list[pos-1]=x;
	return 1;
}
void insertFirstList(struct List *L,ElemType x)
{
	int i;
	if(L->size==L->MaxSize)
	{
		againMalloc(L);
	}
	for(i=L->size-1;i>=0;i--)
	{
		L->list[i+1]=L->list[i];
	}
	L->list[0]=x;
	L->size++;
}
void insertLastList(struct List *L,ElemType x)
{
	if(L->size==L->MaxSize)
	{
		againMalloc(L);
	}
	L->list[L->size]=x;
	L->size++;
}
int insertPosList(struct List *L,int pos,ElemType x)
{
	int i;
	if(pos<1||pos>L->size+1)
	{
		return 0;
	}
	if(L->size==L->MaxSize)
	{
		againMalloc(L);
		for(i=L->size-1;i>pos-1;i++)
		{
			L->list[i+1]=L->list[i];
		}
		L->list[pos-1]=x;
		L->size++;
		return 1;
	}
}
void insertOrderList(struct List *L,ElemType x)
{
	int i,j;
	if(L->size==L->MaxSize)
	{
		againMalloc(L);
	}
	for(i=0;i<L->size;i++)
	{
		if(x<L->list[i]){
			break;
		}
		for(j=L->size-1;j>=i;j--)
		{
			L->list[j+1]=L->list[j];
		}
		L->list[i]=x;
		L->size++;
	}
}
ElemType deleteFirstList(struct List *L)
{
	ElemType temp;
	int i;
	if(L->size==0)
	{
		printf("线性表为空\n");
		exit(1);
	}
	temp=L->list[0];
	for(i=1;i<L->size;i++)
	{
		L->list[i-1]=L->list[i];
	}
	L->size--;
	return temp;
}
ElemType deleteLastList(struct List *L)
{
	if(L->size==0)
	{
		printf("线性表为空,不能进行删除操作\n");
		exit(1);
	}
	L->size--;
	return L->list[L->size];
}
ElemType deletePosList(struct List *L,int pos)
{
	ElemType temp;
	int i;
	if(pos<1 || pos>L->size){
		printf("pos值越界,不能进行删除操作\n");
		exit(1);
	}
	temp=L->list[pos-1];
	for(i=pos;i<L->size;i++)
	{
		L->list[i-1]=L->list[i];
	}
	L->size--;
	return temp;
}
int deleteValueList(struct List *L,ElemType x)
{
	int i,j;
	for(i=0;i<L->size;i++)
	{
		if(L->list[i]==x)
		{
			break;
		}
		if(i==L->size)
		{
			return 0;
		}
		for(j=i+1;j<L->size;j++)
		{
			L->list[j-1]=L->list[j];
		}
		L->size--;
		return 1;
	}
}
int main()
{
	int a[10]={2,4,6,8,10,12,14,16,18,20};
	int i;
	struct List L;
	initList(&L,5);
	for(i=0;i<10;i++)
	{
		insertFirstList(&L,a[i]);
	}
	insertPosList(&L,11,48);
	insertPosList(&L,1,64);
	printf("%d\n",getElem(&L,4));
	traverseList(&L);
	printf("%d\n",findList(&L,10));
	updatePosList(&L,3,20);
	deleteFirstList(&L);
	deleteFirstList(&L);
	deleteLastList(&L);
	deleteLastList(&L);
	deletePosList(&L,5);
	//deletePosList(&L,7);   元素越界了
	printf("%d\n",sizeList(&L));
	printf("%d\n",emptyList(&L));
	traverseList(&L);
	clearList(&L);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值