顺序表应用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;
}
相关文章推荐
- leetcode ----Intersection of Two Arrays
- github被DNS劫持不能连接的问题的解决办法
- DAY12:leetcode #24 Swap Nodes in Pairs
- 网络编程:server服务器转发消息,多个客户端实现群聊
- sdut oj1244 数列有序!(顺序表)
- Interleaving String leetcode
- Linux常用命令
- medrank算法配合B+树实现一个简单预测模型
- Eclipse编译ijkplayer
- 【KMP】BZOJ3942-[Usaco2015 Feb] Censoring
- 头像上传功能实现,PC端的需要做兼容
- 顺序表应用1:多余元素删除之移位算法
- NUBT 1651 Red packet(红包问题,二分,宁波工程学院在线评测)
- 网络中的基本概念
- SQL基础语句
- hd 1969 Pie (二分)
- Parcelable接口
- 1088. Rational Arithmetic (20)
- 浅解:iOS应用程序中的性能优化
- FloatingActionButton 完全解析[Design Support Library(2)]