一、顺序表
1.1 逆置顺序表中任意一段序列
void exchange(int q[], int len, int left, int right) {
if (left >= right || right >= len) return;
int mid = (left + right) >> 1;
for (int i = 0; i <= mid - left; i++) {
int tmp = q[left + i];
q[left + i] = q[right - i];
q[right - i] = tmp;
}
}
1.2 删除顺序表中所有值为 k 的元素
void del_k(int q[], int &len, int k) {
int j = 0;
for (int i = 0; i < len; i++)
if (q[i] != k) q[j++] = q[i];
len = j;
}
1.3 删除重复元素
void del(int q[], int &len) {
int i = 0, j = 1;
for (; j < len; j++)
if (q[i] != q[j])
q[++i] = q[j];
len = i + 1;
}
1.4 修改顺序表中一串数据的顺序
void exchange(int q[], int len, int left, int right) {
if (left >= right || right >= len) return;
int mid = (left + right) >> 1;
for (int i = 0; i <= mid - left; i++) {
int tmp = q[left + i];
q[left + i] = q[right - i];
q[right - i] = tmp;
}
}
void exchange_num(int q[], int len, int m, int n) {
exchange(q, len, 0, len - 1);
exchange(q, len, 0, m - 1);
exchange(q, len, m, len - 1);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
1.5 找出主元素(摩尔投票计数法)