08 线性表的基本概念
对参数的修改结果需要“带回来”就使用引用类型的参数
顺序表的定义:
顺序存储、链式存储
sizeof(int)、sizeof(MyStruct)
顺序表的扩展
虽然大小改变,但是时间开销大
让代码健壮:对数据操作的合法性进行检查,比如长度,元素位置有没有越界,再给一个反馈
时间复杂度都是O(n)
题目:
答案:ACBD
大题:P18
第10题
//10、移位数组
void Reverse(int R[], int from, int to) {
for (int i = from; i <= (from + to) / 2; i++) {
swap(R[from++], R[to--]);
}
}
void Converse(int R[], int n, int p) {
Reverse(R, 0, p - 1);
Reverse(R, p, n - 1);
Reverse(R, 0, n - 1);
}
void printArr(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main() {
int arr1[] = { 0,1,2,3,4,5,6 };
int arr2[] = { 0,1,2,3,4,5,6,7 };
int len1 = sizeof(arr1) / sizeof(int);
int len2 = sizeof(arr2) / sizeof(int);
printArr(arr1, len1);
cout << endl;
Converse(arr1, len1, 3);
printArr(arr1, len1);
}
第13题
#include<iostream>
using namespace std;
#pragma warning(disable:4996)
void printArr(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int findMinNotExist(int arr[], int n) {
int minNum = 0;
bool* flagArr = (bool*)malloc(sizeof(bool) * n);
for (int i = 0; i < n; i++)
flagArr[i] = false;
for (int j = 0; j < n; j++) {
if (arr[j] > 0 && arr[j] <= n) {
flagArr[arr[j] - 1] = true;
}
}
for (int k = 0; k < n; k++) {
if (flagArr[k] == false) {
free(flagArr);
flagArr = NULL;
minNum = k+1;
return minNum;
}
}
free(flagArr);
flagArr = NULL;
return minNum + 1;
}
int main() {
int arr1[] = { 0,1,2,6 };
int arr2[] = { 0,1,2,3,4,5,6,7 };
int len1 = sizeof(arr1) / sizeof(int);
int len2 = sizeof(arr2) / sizeof(int);
int min1=findMinNotExist(arr1, len1);
int min2 = findMinNotExist(arr2, len2);
}