【c语言】【数据结构】【结构体】顺序表增删改查函数的实现

该代码根据以下条件编写:

  1. 空表删除、查询、求长度可否报错;
  2. 添加表首和表尾位置能否成功,其它添加位置能否成功;过小或过大的添加位置能否报错;
  3. 删除表首和表尾位置能否成功,其它删除位置能否成功;过小或过大的删除位置能否报错;
  4. 查找成功和查找失败是否均能正确显示;
    根据严蔚敏数据结构书上的代码加以修改而成。(定位函数和书上的返回值有所不同)
//编写人:naruuu
//编写时间:2020 10 6
//编写功能:顺序表增删改查函数的实现
#include <stdio.h> 
#include <stdlib.h>

#define MAXSIZE 100
#define OK 1
#define ERROR -1
#define OVERFLOW -1

#define ElemType int
#define Status int

//结构体类型定义
typedef struct sqlist 
{
    ElemType *elem;
	int length;
}SqList;


//顺序表初始化
Status InitList (SqList &L)
{
    L.elem=new ElemType[MAXSIZE];
	if(!L.elem) exit(OVERFLOW);
	L.length=0;
	return OK;
}
//查找元素
Status GetElem(SqList L,int i,ElemType &e)
{
	if(L.length==0)
	{
		printf("the SqList is empty\n");
		return ERROR;
	}
	if(i<1||i>L.length)
	{
		printf("the num is too small or too large\n");
		e=0;
		return ERROR;
	}
	e=L.elem[i-1];
	return OK;
}

//定位元素
Status LocateElem(SqList L,ElemType e)
{
    int i;
	for(i=0;i<L.length;i++)
		if(L.elem[i]==e)
		{
			printf("the num's location is:");
			return i+1;
		}
		if(i==L.length)
		{
			printf("Sorry the num doesnt exsit in the SqList\n");
			return 0;
		}
		return OK;

}

//插入元素
Status ListInsert(SqList &L,int i,ElemType e)
{
	int j;
    if(i<1||i>L.length+1) 
	{
		
		printf("error!the num is wrong\n");
		return ERROR;
	}
	if(L.length==MAXSIZE) 
	{
		printf("error!the length is too large ");
		return ERROR;
	}
	else
		printf("success!\n");
	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;
}

//删除元素
Status ListDelete(SqList &L,int i,ElemType &e)
{
    int j;
	if(L.length==0)
	{
		printf("the SqList is empty\n");
		return ERROR;
	}
	if(i<1||i>L.length)
	{
		printf("error!the num is too small or too large \n");
		return ERROR;
	}
	else
		printf("success!\n");
	e=L.elem[i-1];
	for(j=i;j<=L.length-1;j++)
		L.elem[j-1]=L.elem[j];
	--L.length;
	return OK;
}

int main()
{
    SqList L;
	ElemType e;
	int tmp;
	InitList(L);printf("the length is :%d\n",L.length);
	ListDelete(L,1,e);
	LocateElem(L,10);
	GetElem(L,2,e);
	ListInsert(L,1,10);	ListInsert(L,2,20);ListInsert(L,3,30);ListInsert(L,0,10);ListInsert(L,101,30);
	GetElem(L,1,e);printf("%d\n",e);
	GetElem(L,2,e);printf("%d\n",e);
	GetElem(L,3,e);printf("%d\n",e);
	GetElem(L,0,e);printf("%d\n",e);
	GetElem(L,101,e);printf("%d\n",e);
    tmp=LocateElem(L,20);printf("%d\n",tmp);
	tmp=LocateElem(L,40);printf("%d\n",tmp);
	
	ListDelete(L,1,e);printf("%d\n",e);printf("the length is:%d\n",L.length);
	ListDelete(L,2,e);printf("%d\n",e);printf("the length is:%d\n",L.length);
	ListDelete(L,1,e);printf("%d\n",e);printf("the length is:%d\n",L.length);
	ListDelete(L,0,e);printf("%d\n",e);printf("the length is:%d\n",L.length);
	ListDelete(L,4,e);printf("%d\n",e);printf("the length is:%d\n",L.length);

	return 0;
}








运行结果
the length is :0
the SqList is empty
Sorry the num doesnt exsit in the SqList
the SqList is empty
success!
success!
success!
error!the num is wrong
error!the num is wrong
10
20
30
the num is too small or too large
0
the num is too small or too large
0
the num's location is:2
Sorry the num doesnt exsit in the SqList
0
success!
10
the length is:2
success!
30
the length is:1
success!
20
the length is:0
the SqList is empty
20
the length is:0
the SqList is empty
20
the length is:0
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值