简单动态顺序表

简单的动态顺序表需实现的接口函数有增删改查;

一个简单的顺序表就是数组

这里先创建一个顺序表

//动态
#pragma once   //防止头文件被重复包含
//引用的头文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
//重定义int类型
typedef int SQDataType;
//重定义struct SeqList 为 SL
typedef struct SeqList
{
	SQDataType *a;
	int size;		//有效个数
	int capacity;	//容量
}SL;

动态的顺序表较静态顺序表而言,更加的节省空间,不够加扩容,不会造成过多的浪费。

最后需释放扩容的空间。

动态扩容函数

void Realloc(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		//判断capacity是否为0是则newcapacity为4不是则为capacity*2
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        //扩容  newcapacity 个 sizeof(SQDataType) 大小的内存
		SQDataType* tmp = (SQDataType*)realloc(ps->a, sizeof(SQDataType) * newcapacity);
		if (tmp == NULL)
		{
			printf("realloc is fail!\n");
			exit(-1);//结束程序
		}
		else
		{
			ps->a = tmp;
			ps->capacity = newcapacity;
		}
	}
}

释放扩容的空间函数

void SeqListFree(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

删除又包含头删,尾删,任意位置删除。

增加又包含头插,尾插,任意位置插入。

这里依次来实现

首先先将结构体各数据初始化

void SeqListInit(SL* ps)
{
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;

}

头插函数

void SeqListPushFront(SL* ps, SQDataType x)
{
	Realloc(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[0] = x;
	ps->size++;

}

尾插函数

void SeqListPushBack(SL* ps, SQDataType x)
{
	Realloc(ps);
	ps->a[ps->size] = x;
	ps->size++;

}

任意位置插入函数

void SeqListInsert(SL* ps, int pos, SQDataType x)
{
    //判断
	assert(pos <= ps->size);
	Realloc(ps);
	int end = ps->size - 1;
	while (end >= pos - 1)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}

	ps->a[pos - 1] = x;
	ps->size++;
}

尾删函数

void SeqListPopBack(SL* ps)
{
	if (ps->size == 0)
	{
		printf("已空!\n");
		return;
	}
	ps->size--;
}

头删函数

void SeqListPopFront(SL* ps)
{
	//assert(ps->size > 0);   和下面if判断一样
	if (ps->size == 0)
	{
		printf("已空!\n");
		return;
	}
	int start = 1;
	while (start < ps->size)
	{
		ps->a[start - 1] = ps->a[start];;
		++start;
	}
	ps->size--;
}

任意位置删除函数

void SeqListErase(SL* ps, int pos)
{
	assert(pos <= ps->size);
	int start = pos - 1;
	while (start < ps->size)
	{
		ps->a[start] = ps->a[start + 1];
		++start;
	}
	ps->size--;
}

查找函数

void SeqListFind(SL* ps, SQDataType x)
{
	int i = 0;
	int k = 0;
	int tmp = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			k = 1;
			tmp = i;
		}
		
	}
	if (k == 1)
	{
		printf("下标为: %d\n", tmp);
	}
	else
	{
		printf("查找失败!\n");
	}
}

修改函数

void SeqListModity(SL* ps, int pos, SQDataType x)
{
	assert(pos <= ps->size);
	ps->a[pos - 1] = x;
}

打印函数

void SeqListPrint(SL* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; ++i)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

最后写框架

int main()
{
	SL sl;
	SeqListInit(&sl);
	int input = 0;
	do
	{
		int x = 0;
		int pos = 0;
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("请输入要尾插的数字: ");
			scanf("%d", &x);
			SeqListPushBack(&sl, x);
			break;
		case 2:
			printf("请输入要头插的数字: ");
			scanf("%d", &x);
			SeqListPushFront(&sl, x);
			break;
		case 3:
			
			SeqListPopBack(&sl);
			printf("尾删成功!\n");
			break;
		case 4:
			
			SeqListPopFront(&sl);
			printf("头删成功!\n");
			break;
		case 5:
			printf("请选择要删除的位置: ");
			scanf("%d", &pos);
			SeqListErase(&sl, pos);
			printf("删除成功!\n");
			break;
		case 6:
			printf("请选择要插入的位置: ");
			scanf("%d", &pos);
			printf("请输入要插入的数字: ");
			scanf("%d", &x);
			SeqListInsert(&sl, pos, x);
			printf("插入成功!\n");
			break;
		case 7:
			printf("请输入要查找的数字: ");
			scanf("%d", &x);
			SeqListFind(&sl, x);
			break;
		case 8:
			printf("请输入要修改数字的位置: ");
			scanf("%d", &pos);
			printf("请输入修改数字: ");
			scanf("%d", &x);
			SeqListModity(&sl, pos, x);
			printf("修改成功!\n");
			break;
		case 9:
			SeqListPrint(&sl);
			break;
		case 0:
			printf("退出成功!\n");
			break;
		default:
			printf("选择错误!\n");
			break;

		}

	} while (input);

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值