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

cs 复制代码
#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;
}
相关推荐
不知天地为何吴女士1 小时前
Day32| 509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯
算法
小坏坏的大世界1 小时前
C++ STL常用容器总结(vector, deque, list, map, set)
c++·算法
励志要当大牛的小白菜4 小时前
ART配对软件使用
开发语言·c++·qt·算法
qq_513970444 小时前
力扣 hot100 Day56
算法·leetcode
PAK向日葵5 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
爱装代码的小瓶子6 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
爱喝矿泉水的猛男7 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
YuTaoShao7 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
YouQian7727 小时前
Traffic Lights set的使用
算法
快乐飒男8 小时前
哈希表(c语言)
c语言·哈希算法·散列表