有序顺序表去重
从有序顺序表中删除所有其值重复的元素,使表中所有元素的值均不相同。
1.思路
指针遇到不同的元素时开始操作,遇到相同的元素时不变化。i
指针为不同元素的个数,
2.参考代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include "preset.h"
bool listRedundantRemove(SqList &sql) { int black = 0; for (int red = 1; red < sql.length; ++red) { if (sql.elem[red] != sql.elem[red - 1]) { sql.elem[black + 1] = sql.elem[red]; black++; } } sql.length = black + 1; return true; }
int main () { SqList sql = {{1, 2, 2, 3, 4, 4, 4, 5, 5, 9}, 10}; cout << "Current SqList: "; print(sql); if (listRedundantRemove(sql)) cout << "Remove Redundant Elements in SqList: "; print(sql); else cout << "error" << endl; return 0; }
|