顺序表子表逆置
已知在一维数组A[m+n]
中依次存放两个线性表(a1,a2,a3…,am)
和(b1,b2,b3…bn)
,编写函数将数组中两个顺序表的位置互换。
- 顺序表 内部块序 更改
- 已知一个一维数组A[m+n]中依次存放着 两个线性表(a1,a2,a3…am)和(b1,b2,b3…bn)
- 编写一个函数将数组中两个顺序表的位置互换,即将(b1,b2,b3…bn)放在(a1,a2,a3…am)的前面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include "preset.h"
void listReverse(SqList &sql, int left, int right) { while (left < right) { int temp = sql.elem[left]; sql.elem[left] = sql.elem[right]; sql.elem[right] = temp; left++; right--; } }
void test(SqList &sql, int n, int m) { cout << "Reverse all: " << endl; listReverse(sql, 0, sql.length - 1); print(sql);
cout << "Reverse the first " << n << " elements:" << endl; listReverse(sql, 0, n - 1); print(sql);
cout << "Reverse the last " << m << " elements:" << endl; listReverse(sql, sql.length - m, sql.length - 1); print(sql); }
int main() { SqList sql = {{1, 2, 3, 4, 5, 6, 7}, 7}; cout << "current List:" << endl; print(sql); test(sql, 3, 4); return 0; }
|