#include<iostream>
using namespace std;
typedef struct
{
int *elem;
int length;
}SqList;
void CreateList(SqList &L,int n)
{
for(int i=0;i<n;i++)
{
cin>>L.elem[i];
}
L.length=n;
}
void Delete_e(SqList &L,int e)
{
int i=0,j=0;
while(i<L.length)
{
if(L.elem[i]!=e)
{
L.elem[j]=L.elem[i];
i++;
j++;
}else
{
i++;
}
}
L.length=j;
}
void display(SqList &L)
{
for(int i=0; i<L.length; i++)
{
cout<<L.elem[i]<<" ";
}
}
int main()
{
SqList L;
int n;
cout<<"请输入线性表长度:";
cin>>n;
cout<<"请输入线性表元素的值:";
CreateList(L,n);
int e;
cout<<"请输入你想删除的元素:";
cin>>e;
Delete_e(L,e);
cout<<"当前线性表为:";
display(L);
return 0;
}
已知长度为n的线性表A采用书顺序存储结构,请写一个时间复杂度为O(n),空间复杂度为O(1)的算法,该算法可删除线性表中所有值为item的数据元素。
最新推荐文章于 2024-09-01 19:21:12 发布
这段代码展示了如何使用C++创建一个顺序线性表,并根据用户输入删除指定元素。程序首先定义了一个SqList结构体,然后通过CreateList函数输入线性表元素,Delete_e函数删除元素,最后用display函数展示当前线性表。

1万+





