顺序表的增删改查等操作以及动态扩容

定义一个结构体:

typedef struct SeqList {
	ElemType* base;
	size_t capacity;
	size_t size;
}SeqList;

1.对数组初始化

void SeqListInit(SeqList* pst, size_t capacity) {
	pst->capacity = capacity;
	pst->base = (ElemType*)malloc(sizeof(ElemType) * pst->capacity);
	assert(pst->base != NULL);
	memset(pst->base, 0, sizeof(ElemType) * pst->capacity);
	pst->size = 0;
}

2.顺序表 尾插 push_back

void SeqListPushBack(SeqList* pst, ElemType v)
{
	//检查容量
	if (_IsFull(pst) && !_Inc(pst))
	{
		printf("顺序表容量不足,%d 不能插入.\n", v);
		return;
	}

	pst->base[pst->size++] = v;
}

3.顺序表 头插 push_front

void SeqListPushFront(SeqList* pst, ElemType v) {
	if (_IsFull(pst) && !_Inc(pst))//容量不够自动扩容
	{
		printf("顺序表容量不足,%d 不能插入.\n", v);
		return;
	}
	for (int i = pst->size; i > 0; i--) {
		pst->base[i] = pst->base[i - 1];
	}
	pst->base[0] = v;
	pst->size++;
}

4.顺序表尾 删除  pop_back

void SeqListPopBack(SeqList* pst) {
	if (_IsEmpty(pst))
	{
		printf("顺序表已空,不能删除.\n");
		return;
	}
	pst->size--;
}

5.顺序表 头删 pop_front

void SeqListPopFront(SeqList* pst) {
	if (_IsEmpty(pst))
	{
		printf("顺序表已空,不能删除.\n");
		return;
	}
	for (int i = 0; i < pst->size-1; i++) {
		pst->base[i-1] = pst->base[i];
	}
	pst->size--;
}

 6.(1)bool _Inc(SeqList* pst)容量不够自动扩容

    (2)bool _IsFull(SeqList* pst) 数组容量已满

    (3)bool _IsEmpty(SeqList* pst)数组为空

#define INC_SIZE 3
bool _Inc(SeqList* pst)
{
	pst->base = (ElemType*)realloc(pst->base, sizeof(ElemType) * (pst->capacity + INC_SIZE));
	if (pst->base == NULL)
		return false;
	pst->capacity += INC_SIZE;
	return true;
}
bool _IsFull(SeqList* pst)
{
	return pst->size >= pst->capacity;
}
bool _IsEmpty(SeqList* pst)
{
	return pst->size == 0;
}

7.打印顺序表 show_list

void SeqListShow(SeqList* pst) {
	for (int i = 0; i < pst->size; i++) {
		printf(" %d ", pst->base[i]);
	}
	printf("\n");
}

8.顺序表按照下标插入 insert_pos

void SeqListInsertByPos(SeqList* pst, int pos, ElemType* v) {//位置插入
	if (_IsFull(pst) && !_Inc(pst))
	{
		printf("顺序表容量不足,%d 不能插入.\n", v);
		return;
	}

	if (pos<0 || pos>pst->size) {
		printf("插入的位置非法,%d 不能插入.\n", v);
		return;
	}
	//插入数据
	for (int i = pst->size; i > pos; --i)
		pst->base[i] = pst->base[i - 1];
	pst->base[pos] = v;
	pst->size++;
}

9.顺序表按照值插入  insert_val

void SeqListInsertByVal(SeqList* pst, ElemType v) {
	int pos = 0;
	while (v > pst->base[pos]) {
		pos++;
	}
	SeqListInsertByPos(&pst, pos,v);
}

 10.通过顺序表的下标找到对应的值 find_pos

int SeqListFindByPos(SeqList* pst, int pos) {
	assert(pos >= 0 && pos < pst->size);
	return pst->base[pos];
}

 11.通过值找下标 find_val

int SeqListFindByVal(SeqList* pst, ElemType v)
{
	for (size_t i = 0; i < pst->size; ++i)
	{
		if (pst->base[i] == v)
		{
			return i;
		}
	}
	return -1;
}

12.按照下标所在位置删除  erase_pos 

void SeqListEraseByPos(SeqList* pst, size_t pos)
{
	assert(pst && pos < pst->size);

	size_t start = pos;
	while (start < pst->size-1)
	{
		pst->base[start] = pst->base[start + 1];
	++start;
	}
	pst->size--;
}

13.按照值来删除 erase_val 

void SeqListEraseByVal(SeqList* pst, ElemType key) {
	int pos = SeqListFindByVal(pst, key);
	if (pos == -1) {
		printf("要删除的值不在!\n");
		return;
	}
	SeqListEraseByPos(&pst, pos);
}

14.求顺序表的长度和容量 length  capacity

size_t SeqListLength(SeqList* pst) {
	return pst->size;
}
size_t SeqListcapacity(SeqList* pst) {
	return pst->capacity;
}

15.对顺序表进行排序 sort

void SeqListSort(SeqList* pst) {
	bool is_swp = false;
	int  tmp;
	for (int i = 0; i < pst->size - 1; i++) {
		for (int j = pst->size - 1; j > i; j--) {
			if (pst->base[j - 1] > pst->base[j]) {
				tmp = pst->base[j - 1];
				pst->base[j-1] = pst->base[j];
			    pst->base[j]=tmp;
				is_swp = true;
			}
		}
		if(!is_swp){
			break;
		}
	}
}

16.顺序表逆置 reverse

void SeqListReverse(SeqList* pst) {
	int left = 0;
	int right = pst->size - 1;
	while (left < right) {
		int tmp = pst->base[left];
		pst->base[left] = pst->base[right];
		pst->base[right] = tmp;
		left++;
		right--;
	}
}

17.顺序表 清空 clear

void SeqListClear(SeqList* pst) {
	pst->size =0;
	printf("表已清空!\n");
}

18.顺序表销毁 destroy

void SeqListDestroy(SeqList* pst)
{
	free(pst->base);
	pst->base = NULL;
	pst->capacity = pst->size = 0;
}

主函数:

int main(int argc, char* argv[])
{
	SeqList mylist;
	SeqListInit(&mylist, 8);

	ElemType item;
	int pos = 0;
	int val = 0;
	int select = 1;
	while (select)
	{
		printf("********************************************\n");
		printf("* [1] push_back           [2] push_front   *\n");
		printf("* [3] show_list           [0] quit_system  *\n");
		printf("* [4] pop_back            [5] pop_front    *\n");
		printf("* [6] insert_pos          [7] insert_val   *\n");
		printf("* [8] erase_pos           [9] erase_val    *\n");
		printf("* [10] find_val           [11] find_pos    *\n");
		printf("* [12] sort               [13] reverse     *\n");
		printf("* [14] length             [15] capacity    *\n");
		printf("* [16] clear              [17] destroy     *\n");
		printf("********************************************\n");
		printf("请选择:>");
		scanf("%d", &select);
		if (select == 0)
			break;
		switch (select)
		{
		case 1:
			printf("请输入要插入的数据<以-1结束>:> \n");
			while (scanf("%d", &item), item != -1)
			{
				SeqListPushBack(&mylist, item);
			}
			break;
		case 2:
			printf("请输入要插入的数据<以-1结束>:>");
			while (scanf("%d", &item), item != -1)
			{
				SeqListPushFront(&mylist, item);
			}
			break;
		case 3:
			SeqListShow(&mylist);
			break;
		case 4:
			SeqListPopBack(&mylist);
			break;
		case 5:
			SeqListPopFront(&mylist);
			break;
		case 6:
			printf("请输入要插入的位置:>");
			scanf("%d", &pos);
			printf("请输入要插入的值:>");
			scanf("%d", &item);
			SeqListInsertByPos(&mylist, pos, item);
			break;
		case 7:
			printf("请输入要插入的值:>");
			scanf("%d", item);
			SeqListInsertByVal(&mylist, item);
			break;
		case 8:
			printf("请输入要删除的位置:>");
			scanf("%d", &pos);
			SeqListEraseByPos(&mylist, pos);
			break;
		case 9:
			printf("请输入要删除的值:>");
			scanf("%d", val);
			SeqListEraseByVal(&mylist, val);
			break;
		case 10:
			printf("请输入要查找的值:>");
			scanf("%d",&item);
			int ret=SeqListFindByVal(&mylist, item);
			printf(" %d 的下标= %d\n",item, ret);
			break;
		case 11:
			printf("请输入要找的位置:>");
			scanf("%d", &pos);
			int cur = SeqListFindByPos(&mylist, pos);
			printf(" %d 位置的值为 %d\n", pos, cur);
			break;
		case 12:
			SeqListSort(&mylist);
			break;
		case 13:
			SeqListReverse(&mylist);
			break;
		case 14:
			printf("顺序表的长度为:> %d\n", SeqListLength(&mylist));
			break;
		case 15:
			printf("顺序表的容量为:> %d\n", SeqListcapacity(&mylist));
			break;
		case 16:
		    SeqListClear(&mylist);
			break;
		case 17:
			SeqListDestroy(&mylist);
			break;
		default:
			break;
		}
	}
	SeqListDestroy(&mylist);
	printf("GoodBye.\n");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值