有序顺序表去重
有序顺序表去重
从有序顺序表中删除所有其值重复的元素,使表中所有元素的值均不相同。
1.思路指针遇到不同的元素时开始操作,遇到相同的元素时不变化。i指针为不同元素的个数,
2.参考代码12345678910111213141516171819202122#include "preset.h"//listRedundantRemove冗余数据去除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;}
有序顺序表合并
有序顺序表合并
将两个有序表合并为一个新的有序顺序表,并由函数返回结果顺序表。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263//[顺序表合并]将两个(有序)顺序表 合并为一个 新的(有序)顺序表,并由函数返回结果顺序表#include "preset.h"//顺序表合并(利用三个int类型指针i\j\k)bool merge(SqList a, SqList b, SqList &c) { if (c.length < a.length + b.length) return false; int i = 0, j = 0, k = 0; //逐个比较 将较小的插入顺序表c中 while (i < a.length && j < b.length) { if (a.elem[i] <= b.elem[j]) { c.elem[k] = a.elem[i]; i++; k++; } else { c.elem[k] = b.elem[j]; j++; k++; } } //剩余元素直接插入结果 while (i < a.length) { c.elem[k] = a.elem[i]; k++; i++; } while (j < b.length) { c.elem[k] = b.elem[j]; k++; j++; } //修改顺序表c的长度 c.length = a.length + b.length; return true;}//顺序表合并(利用三个int*类型指针pa\pb\pc)bool mergeList(SqList La, SqList Lb, SqList &Lc) ...
顺序表子表逆置
顺序表子表逆置
已知在一维数组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)的前面
123456789101112131415161718192021222324252627282930313233#include "preset.h"//顺序表逆置void listReverse(SqList &sql, int left, int right) { //int p = 0; //int q = sql.length - 1; while (left < right) { int temp = sql.elem[left]; sql.elem[left] = sql.elem[right]; sql.elem[right] = temp; left++; right--; }}//逆置前n个元素、逆置后m个元素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:&quo ...
顺序表综合操作
顺序表综合操作
线性表(a1,a2,a3…an)中的元素递增有序且按顺序存储与计算机内,
要求设计一个算法完成用最少时间在表中查找数值为x的元素,若找到则将其与后继元素位置相互交换,若找不到则将其插入表中并使表中元素仍然递增有序
1.二分查找递归12345678910111213141516171819202122232425#include "preset.h"//有序顺序表查找(二分查找)递归写法int binSearch(SqList sql, int target, int low, int high) { if (low > high) return -1; int mid = (low + high) / 2; cout << "low : " << low << " high : " << high << " mid : " << mid << endl; if (target == sql.elem[mid]) { return mid; } else if (target < sql.elem[mid]) { high = mid - 1; binSearch(sql, target, low, high); } else { low = mid + 1; binSearch(sql, target, low, high); }}int main() { SqList sql = {{1, 3, 5, 7, 8, 10, 11}, 7}; cout << "current List:" << endl; print(sql); int target; cin >> target; cout << binSearch(sql, target, 0 ...