您的位置:首页 > 其它

顺序表应用2:多余元素删除之建表算法

2016-07-27 11:04 363 查看
#include<iostream>
#include<algorithm>

using namespace std;

const int maxn=10010;

typedef struct
{
int *emue;
int lengh;
}lis;

void initlist(lis *L)
{
L->emue=new int [maxn];
L->lengh=0;
}

void creat(lis &L, int k)
{
L.lengh=k;
for(int i=0;i<L.lengh;i++)
{
cin>>L.emue[i];
}
}

void del(lis &L, int k)
{
for(int i=k;i<L.lengh-1;i++)
L.emue[i]=L.emue[i+1];
L.lengh--;
}

void loclist(lis &L)
{
for(int i=0;i<L.lengh-1;i++)
for(int j=i+1;j<L.lengh;j++)
{
if(L.emue[i]==L.emue[j])
{
del(L, j);
j--;
}
}
}

void mov(lis &L, int n, int m)
{
for(int i=0;i<m;i++)
L.emue[i+n]=L.emue[i];
}

void creatdel(lis &L, int m)
{
L.emue=new int [m];
L.lengh=0;
int num=0;
int x, flag;
while(num<m)
{
cin>>x;
num++;
if(L.lengh==0)
{
L.emue[0]=x;
L.lengh++;
}
else
{
flag=1;
for(int i=0;i<L.lengh;i++)
{
if(L.emue[i]==x)
{
flag=0;
break;
}
}
if(flag)
{
L.emue[L.lengh]=x;
L.lengh++;
}
}
}
}
int main()
{
lis L;
int n, m;
cin>>n;
while(n--)
{
cin>>m;
//      initlist(&L);                                   ///���顺�表
creatdel(L, m);                                    ///�顺�表��
for(int i=0;i<L.lengh-1;i++)
cout<<L.emue[i]<<" ";
cout<<L.emue[L.lengh-1]<<endl;
}
return 0;
}

没想到两月之后再写,算法套路竟然一样。

#include<iostream>
#include<algorithm>

using namespace std;

const int maxn=10000+1000;

typedef struct
{
int* data;
int length;

}List;

void initlist(List &L)
{
L.data=new int [maxn];
}

void clean(List &L)
{
L.length=0;
}

void creat_del_list(List &L, int n)
{
int num=0, x, flag;
while(num<n)
{
flag=1;
cin>>x;
if(!L.length)
{
L.data[0]=x;
L.length++;
}
else
{
for(int i=0; i<L.length;++i)
{
if(L.data[i]==x)
{
flag=0;
break;
}
}
if(flag)
{
L.data[L.length]=x;
L.length++;
}
}
num++;
}
}

void display(List &L)
{
for(int i=0;i<L.length;++i)
i==0? cout<<L.data[i] : cout<<" "<<L.data[i];
cout<<endl;
}

int main()
{
ios::sync_with_stdio(false);
int t, n;
List L;
cin>>t;
initlist(L);
while(t--)
{
clean(L);
cin>>n;
creat_del_list(L, n);
display(L);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: