线性表经典题

已知长度为n (n<20) 的线性表A采用顺序存储结构,请写一时间复杂度为0(n)、空间复杂度为0(1)的算法,该算法删除线性表中所有值为item的数据元素。(O(1)表示算法的辅助空间为常量)。
本人初学者,代码有个人风格,不过还是很好懂的,请见谅。

#include
using namespace std;
typedef int LeleType;
#define MaxSize 100

typedef struct
{
LeleType *Lele;
int length;

}SeqList;

int FirstList(SeqList &L)
{
int i;
int len;
L.Lele = new LeleType[MaxSize];
if(!L.Lele)
{
return 0;
}
cin >> len;
L.length = len;
for(i = 0; i<L.length; i++)
{
cin >> L.Lele[i];
}
}

void ListDelete(SeqList &L,int item)
{
int i;
int j = 0;
for(i=0; i<L.length;i++)
{
if(L.Lele[i] != item)
{
L.Lele[j] = L.Lele[i];
j++;
}
}
L.length = j;
}
int main()
{
SeqList L;
int n;
FirstList(L);
cin >> n;
ListDelete(L,n);
for(int i=0;i<L.length;i++)
cout << L.Lele[i] << endl;
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值