不定长顺序表

顺序表在生活中不怎么使用,人们一般用顺序表的话,我感觉就都用成了数组。

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

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

#define TRUE 1
#define FALSE 0
#define	OK 1
#define	ERROR 0
#define INFEASIBLE -1
#define OVERLOW -2

typedef int Status;
typedef int ElemType;
typedef struct
{
	ElemType* elem;
	int length;
	int listsize;
}Sqlist;

bool IsFull(Sqlist* L)
{
	assert(L != nullptr);
	return L->length >= L->listsize;
}
Status InitList_Sq(Sqlist* L)
{
	assert(L != nullptr);
	L->elem = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
	if (L->elem == nullptr) exit(1);
	L->length = 0;
	L->listsize = LIST_INIT_SIZE;
	return OK;
}
void Reward(Sqlist* L)
{
	assert(L != nullptr);
	ElemType* newbase = (ElemType*)realloc(L->elem, sizeof(ElemType) * (L->listsize + LISTINCREMENT));
	if (newbase == nullptr) exit(OVERLOW);
	L->elem = newbase;
	L->listsize = L->listsize += LISTINCREMENT;
}
Status Insert_Sq2(Sqlist* L, int pos, int val)
{
	assert(L != nullptr);
	if (pos<1 || pos>L->length + 1) return ERROR;
	if (IsFull(L)) Reward(L);
	for (int i = L->length - 1; i >= pos - 1; --i)
	{
		L->elem[i + 1] = L->elem[i];
	}
	L->elem[pos - 1] = val;
	L->length += 1;
	return OK;
}
Status Delete_Sq(Sqlist* L, int pos)
{
	assert(L != nullptr);
	if (pos<1 || pos>L->length) exit(1);
	for (int i = pos; i <= L->length-1; ++i)
	{
		L->elem[i-1] = L->elem[i];
	}
	L->length -= 1;
	return OK;
}
Status Resarch_Sq(Sqlist* L, int val)
{
	assert(L != nullptr);
	for (int i = 0; i <= L->length - 1; ++i)
	{
		if (L->elem[i] == val)
		{
			return i + 1;
		}
	}
	return ERROR;
}
Status Updata_Sqval(Sqlist* L, int val, int newval)
{
	assert(L != nullptr);
	int pos = Resarch_Sq(L, val);
	L->elem[pos - 1] = newval;
	return OK;
}
Status Updata_Sqpos(Sqlist* L, int pos, int newval)
{
	assert(L != nullptr);
	L->elem[pos - 1] = newval;
	return OK;
}
void Print_Sq(Sqlist* L)
{
	assert(L != nullptr);
	for (int i = 0; i < L->length; ++i)
	{
		printf("%d ", L->elem[i]);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值