标签:
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INIT_SIZE 10
#define INCREMENT_SIZE 10
typedef int Elemtype;
typedef int Status;
typedef struct {
	Elemtype *base;
	int size;
	int length;
}SqList;
Status InitSqList(SqList *L)
{
	L->base=(Elemtype *)malloc(INIT_SIZE*sizeof(Elemtype));
	if(!L->base)
	{
		printf("init_false:\n");
		return ERROR;
	}
	L->size=INIT_SIZE;
	L->length=0;
	return OK;
}
Status InsertElem(SqList *L,int i,Elemtype e)
{
	if(i<1||i>L->length+1)
	{
		return ERROR;
	}
	if(L->length>=L->size)
	{
		L->base=(Elemtype *)realloc(L->base,(L->size+INCREMENT_SIZE)*sizeof(Elemtype));
			if(!L->base)
			{
				printf("insert_fail\n");
				return ERROR;
			}
			L->size+=INCREMENT_SIZE;
	}
	Elemtype *q=&L->base[L->length-1];
	Elemtype *p=&L->base[i-1];
	for(;q>=p;q--)
	{
		*(q+1)=*q;
		
	}
	*p=e;
	L->length++;
	return OK;
}
Status TraverseList(SqList *L)
{
	int i;
	for(i=0;i<L->length;i++)
	printf("%d\n",L->base[i]);
}
int main()
{	SqList L;
	InitSqList(&L);
	for(int i=0;i<10;i++)
	InsertElem(&L,i+1,i);
	TraverseList(&L);
	return 0;
}
标签:
原文地址:http://www.cnblogs.com/loveyan/p/4556597.html