数据结构之顺序存储结构的线性表中删除第i个元素起的k个元素

#include
#include
#define N 50
struct SeqList
{
	int data[N];
	int length;
};

//初始化
void init_seqlist(struct SeqList *);

//插入
void insert_seqlist(struct SeqList *,int,int); 

//表长度
int length_seqlist(struct SeqList *); 

//删除第i个元素起的k个元素
void delete_seqlist(struct SeqList *,int,int); 
 
//显示结果
void show_seqlist(struct SeqList *); 

void main()
{
	struct SeqList list,*fp;
	int len,i,k;
	fp=&list;
	init_seqlist(fp);
	insert_seqlist(fp,1,10);
	insert_seqlist(fp,2,20);
	insert_seqlist(fp,3,30);
	insert_seqlist(fp,4,40);
	len=length_seqlist(fp); 
	printf("请输入正整数i:");
	scanf("%d",&i);
	printf("请输入正整数k:");
	scanf("%d",&k);
	if(!(i>0 && i<=k && (i+k)<=len+1))
	{
		printf("输入格式错误!\n");
	}else
	{
		delete_seqlist(fp,i,k);
		show_seqlist(fp);
	}
}

void init_seqlist(struct SeqList *fp)
{
	fp->length=0;
}

void insert_seqlist(struct SeqList *fp,int i,int e)
{
	int k;
	for(k=i;klength+1;k++)
	{
		fp->data[k]=fp->data[k-1];
	}
	fp->data[i-1]=e;
	fp->length++;
}

int length_seqlist(struct SeqList *fp)
{
	return fp->length;
}

void show_seqlist(struct SeqList *fp)
{
	int i;
	if(fp->length==0)
	{
		printf("该表为空表!\n");
	}else
	{
		printf("该表为:");
		for(i=0;ilength-1;i++)
		{
			printf("(%d)->",fp->data[i]);
		}
		printf("(%d)",fp->data[i]);
	}
}

void delete_seqlist(struct SeqList *fp,int i,int k)
{
	int g;
	if(fp->length==0)
	{
		printf("该表为空表,删除失败!\n");
	}else
	{
		for(g=i+k;g<=fp->length;g++)
		{
			fp->data[g-k-1]=fp->data[g-1];
		}
		fp->length-=k;
	}
}

你可能感兴趣的:(技术爱好)