数据结构-线性表

#include<stdio.h>
#include<malloc.h>

#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
#define OK 1
#define NO 0 
#define ERROR 0

typedef int Status; 
typedef int Boolean; 
typedef int ElemType; 

typedef struct{
	ElemType *elem;
	int length;
	int listsize; 
}SqList;		//create Linear.

Status InitList(SqList *L) //initilize the Linear.
{
	L->elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
	if(!L->elem){
		printf("OVERFLOW.");
		return ERROR;
	}
	L->length = 0;
	L->listsize = LIST_INIT_SIZE;
	return OK;
}

Status LocateElem(SqList *L,ElemType e)		//select the elements.
{
	ElemType *p;
	int i = 1;
	p = L->elem;
	while(i<=L->length && *(p++)!=e){
		i++;
	}
	if(i<=L->length)
		return i;
	else
		return NO;
}

Status ListInsert(SqList *L,int i,ElemType e)		//insert the elements.
{
	ElemType *newbase,*q,*p;
	int j;
	if(i<1||i>L->length+1)
		return ERROR;
	if(L->length>=L->listsize){
		newbase = (ElemType*)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
		if(!newbase){
			printf("The freedom is full.");
			return ERROR;
		}
		L->elem = newbase;
		L->listsize += LISTINCREMENT;
	}
	for(j=(L->length)-1;j>=i-1;j--){
		L->elem[j+1] = L->elem[j];
	}
	L->elem[i-1] = e;
	(L->length)++;
	return OK;
}

ElemType ListDelete(SqList *L,int i,ElemType e)		//Delete the elmements;
{
	ElemType *p;
	int j;
	if(i<1||i>L->length){
		printf("The elements is not exist.");
		return ERROR;
	}
	e = L->elem[i-1];
	for(j=i-1;j<=L->length-1;j++){
		L->elem[j] = L->elem[j+1];
	}
	L->length += 1;
	return e;
}

int main()
{
	SqList L;
	InitList(&L);
	int i;
	ElemType e;
	for(i=1;i<=5;i++){
		ListInsert(&L,i,i);
	}
	for(i=1;i<=5;i++){
		printf("%d\n",LocateElem(&L,i));
	}
	ListDelete(&L,2,e);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是洋洋a

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值