1. 顺序表取最值
取最大值策略: 用max记录最大值, maxIndex记录最大值的数组索引. max初始值为数组的第一个数据, maxIndex初始值为0. 然后遍历数组, 如果遍历到的数组数据arr[ i ]大于max, 则更新max的值为arr[ i ], 更新maxIndex的值为 i .
取最小值同理.
#include <iostream>
#include <random>
/// <summary>
/// 数组最大长度
/// </summary>
const int MAX_SIZE = 10;
/// <summary>
/// 初始化数组
/// </summary>
/// <param name="arr">数组</param>
/// <param name="length">数组长度</param>
/// <returns></returns>
int initArr(int* arr, int length) {
if (length > MAX_SIZE || length <= 0) {
return 0;
}
// 创建一个随机数引擎
std::random_device rd;
std::mt19937 gen(rd());
// 定义随机数分布范围
std::uniform_int_distribution<> dis(1, 100); // 生成1到100之间的随机整数
for (int i = 0; i < length; i++)
{
arr[i] = dis(gen);
}
return 1;
}
/// <summary>
/// 输出数组元素
/// </summary>
/// <param name="arr"></param>
/// <param name="length"></param>
void printArr(int* arr, int length) {
for (int i = 0; i < length; i++)
{
printf("%d\n", arr[i]);
}
}
/// <summary>
/// 取最大值
/// </summary>
/// <param name="arr">数组</param>
/// <param name="length">数组长度</param>
/// <param name="max">数组最大值</param>
/// <param name="maxIndex">数组最大值的索引</param>
void getMax(int* arr, int length, int& max, int& maxIndex) {
if (length < 0 || length > MAX_SIZE) {
return;
}
//取最大值核心代码
max = arr[0];
maxIndex = 0;
for (int i = 0; i < length; i++)
{
if (arr[i] > max) {
max = arr[i];
maxIndex = i;
}
}
}
/// <summary>
/// 取最小值
/// </summary>
/// <param name="arr">数组</param>
/// <param name="length">数组长度</param>
/// <param name="min">最小值</param>
/// <param name="minIndex">最小值索引</param>
void getMin(int* arr, int length, int& min, int& minIndex) {
if (length < 0 || length > MAX_SIZE) {
return;
}
//取最小值核心代码
min = arr[0];
minIndex = 0;
for (int i = 0; i < length; i++)
{
if (arr[i] < min) {
min = arr[i];
minIndex = i;
}
}
}
int main()
{
int arr[MAX_SIZE];
int length = 10;
initArr(arr, length);
printf("数组元素: \n");
printArr(arr, length);
int max = 0;
int maxIndex = 0;
getMax(arr, length, max, maxIndex);
printf("最大值: %d\n", max);
printf("最大值索引: %d\n", maxIndex);
int min = 0;
int minIndex = 0;
getMin(arr, length, min, minIndex);
printf("最小值: %d\n", min);
printf("最小值索引: %d\n", minIndex);
}
代码1: 顺序表取最值
//取最大值核心代码
max = arr[0];
maxIndex = 0;
for (int i = 0; i < length; i++)
{
if (arr[i] > max) {
max = arr[i];
maxIndex = i;
}
}
代码2: 顺序表取最大值核心代码
//取最小值核心代码
min = arr[0];
minIndex = 0;
for (int i = 0; i < length; i++)
{
if (arr[i] < min) {
min = arr[i];
minIndex = i;
}
}
代码3: 顺序表取最小值核心代码
2. 链表取最值
取最大值策略: 用指针p遍历链表, 用指针q记录最大值的结点, max记录最大值. p, q的初始值均为头结点的下一个结点, 即 q = p = H->next; max的初始值为H->next->data; 对链表进行遍历如果max < p->data; 则更新max为p->data, 更新 q 为 p.
取最小值同理.
#include <iostream>
/// <summary>
/// 链表结点结构体定义
/// </summary>
typedef struct LNode {
int data;
struct LNode* next;
}LNode;
/// <summary>
/// 获取链表的最大值结点
/// </summary>
/// <param name="H">链表的头结点</param>
/// <returns></returns>
LNode* getMax(LNode* H) {
if (H->next == NULL) {
return NULL;
}
LNode* p;
LNode* q;
int max = H->next->data;
p = H->next;
q = H->next;
while (p != NULL)
{
if (max < p->data) {
max = p->data;
q = p;
}
p = p->next;
}
return q;
}
/// <summary>
/// 获取链表最小值结点
/// </summary>
/// <param name="H">链表的头结点</param>
/// <returns></returns>
LNode* getMin(LNode* H) {
if (H->next == NULL) {
return NULL;
}
LNode* p;
LNode* q;
int min = H->next->data;
p = H->next;
q = H->next;
while (p != NULL)
{
if (min > p->data) {
min = p->data;
q = p;
}
p = p->next;
}
return q;
}
int main()
{
LNode* H = (LNode*)malloc(sizeof(LNode));
LNode* A = (LNode*)malloc(sizeof(LNode));
LNode* B = (LNode*)malloc(sizeof(LNode));
LNode* C = (LNode*)malloc(sizeof(LNode));
LNode* D = (LNode*)malloc(sizeof(LNode));
H->data = NULL;
A->data = 12;
B->data = 465;
C->data = 23;
D->data = 6;
H->next = A;
A->next = B;
B->next = C;
C->next = D;
D->next = NULL;
LNode* max = getMax(H);
if (max != NULL) {
printf("链表最大值为: %d\n", max->data);
}
LNode* min = getMin(H);
if (min != NULL) {
printf("链表最小值为: %d\n", min->data);
}
}
代码4: 链表取最值
LNode* p;
LNode* q;
int max = H->next->data;
p = H->next;
q = H->next;
while (p != NULL)
{
if (max < p->data) {
max = p->data;
q = p;
}
p = p->next;
}
return q;
代码5: 链表取最大值核心代码
LNode* p;
LNode* q;
int min = H->next->data;
p = H->next;
q = H->next;
while (p != NULL)
{
if (min > p->data) {
min = p->data;
q = p;
}
p = p->next;
}
return q;
代码6: 链表取最小值核心代码