已知长度为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;
}
相关推荐
再睡一夏就好2 分钟前
【C++闯关笔记】STL:list 的学习和使用
c语言·数据结构·c++·笔记·算法·学习笔记
Ka1Yan10 分钟前
MySQL索引优化
开发语言·数据结构·数据库·mysql·算法
AndrewHZ30 分钟前
【图像处理基石】图像预处理方面有哪些经典的算法?
图像处理·python·opencv·算法·计算机视觉·cv·图像预处理
阿维的博客日记1 小时前
LeetCode 209 - 长度最小的子数组算法详解
数据结构·算法·leetcode
limengshi1383922 小时前
人工智能学习:LR和SVM的联系与区别?
人工智能·算法·机器学习·支持向量机
学习噢学个屁4 小时前
基于STM32的交通灯设计—紧急模式、可调时间
c语言·stm32·单片机·嵌入式硬件
点云SLAM7 小时前
PyTorch 中.backward() 详解使用
人工智能·pytorch·python·深度学习·算法·机器学习·机器人
only-qi7 小时前
146. LRU 缓存
java·算法·缓存
潼心1412o8 小时前
C语言(长期更新)第15讲 指针详解(五):习题实战
c语言·开发语言
梁辰兴9 小时前
数据结构:排序
数据结构·算法·排序算法·c·插入排序·排序·交换排序