已知长度为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;
}
相关推荐
重生之我在20年代敲代码6 分钟前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
X同学的开始1 小时前
数据结构之二叉树遍历
数据结构
limingade2 小时前
手机实时提取SIM卡打电话的信令和声音-新的篇章(一、可行的方案探讨)
物联网·算法·智能手机·数据分析·信息与通信
2401_858286113 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
AIAdvocate4 小时前
Pandas_数据结构详解
数据结构·python·pandas
jiao000015 小时前
数据结构——队列
c语言·数据结构·算法
kaneki_lh5 小时前
数据结构 - 栈
数据结构
铁匠匠匠5 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
C-SDN花园GGbond5 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处6 小时前
C++ —— 关于vector
开发语言·c++·算法