已知长度为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;
}
相关推荐
w_zero_one11 分钟前
算法--构造
java·数据结构·算法
其实防守也摸鱼42 分钟前
信创安全竞赛知识总结与题库
大数据·网络·人工智能·算法·安全·php
揽星河..1 小时前
【C语言】结构体
c语言·开发语言
小超人诞生日记2 小时前
C++ 智能指针的使用及其原理详解
c++·算法
唐青枫3 小时前
C 语言 malloc() 详解与实战 Demo
c语言
lisw054 小时前
互联网上的数据结构可视化!
数据结构·信息可视化
AiNightVision5 小时前
无人机夜视模块如何选择?——从参数指标到系统集成的完整选型指南
人工智能·算法·计算机视觉·车载系统·无人机·视频·智能硬件
牧羊人.3336 小时前
卷积神经网络之VGG
人工智能·深度学习·神经网络·算法·cnn
揽星河..7 小时前
【C语言】指针
c语言·开发语言