顺序表中重复数据的删除
1 #include <iostream> 2 #include <cstdlib> 3 using namespace std; 4 5 int main() 6 { 7 int *data, n, x, k; 8 cin>>n; 9 10 data = (int*)malloc(n*sizeof(int)); 11 12 for (int i = 0; i < n; ++i) 13 { 14 cin>>data[i]; 15 } 16 17 k = 0; 18 cin>>x; 19 for (int i = 0; i < n; ++i) 20 { 21 if(data[i] != x) 22 { 23 data[k] = data[i]; 24 k++; 25 } 26 } 27 28 if(k == 0) 29 { 30 cout<<-1; 31 return 0; 32 } 33 34 for (int i = 0; i < k; ++i) 35 { 36 cout<<data[i]<<" "; 37 } 38 39 return 0; 40 }