已知长度为n的线性表A采用顺序存储结构,请写一时间复杂度为O(n)、空间复杂度为O(1)的算法,该算法删除线性表中所有值为item的数据元素

#include<assert.h>
typedef int SXBint;
typedef struct SL
{
	SXBint* a;
	int size;
	int capacity;
}SLnode;
void SeqLsitInit(SLnode* ps)
{
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
void kuorong(SLnode* ps)
{
	if (ps->capacity == ps->size)
	{
		int Newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SXBint* temp = (SXBint*)realloc(ps->a, sizeof(SLnode) * Newcapacity);
		if (temp == NULL)
		{
			perror("error:");
			exit(1);
		}
		ps->a = temp;
		ps->capacity = Newcapacity;
	}
}
void SeqPushback(SLnode* ps, SXBint x)
{
	kuorong(ps);
	ps->a[ps->size++] = x;
} 
void Seq_dayin(SLnode ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d->", ps.a[i]);
	}
	printf("NULL\n");
}
void deletes(SLnode* ps, int item) {
	int j = 0;
	for (int i = 0; i <= ps->size;i++) {
		if (ps->a[i] != item)
		{
			ps->a[j] = ps->a[i];
			++j;
		}
	}
	ps->size = j - 1;
}

int main()
{
	SLnode S;
	SeqLsitInit(&S);
	int n, num, item;
	printf("请输入顺序表的长度\n");
	scanf("%d", &n);
	printf("请输入顺序表的元素\n");
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &num);
		SeqPushback(&S, num);
	}
	printf("顺序表的元素如下\n");
	Seq_dayin(S);

	printf("请输入item的值\n");
	scanf("%d", &item);
	deletes(&S, item);
	printf("删除item后\n");
	Seq_dayin(S);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值