第七题代码:

#include <iostream>
 
struct node {
    int data;
    struct node *next;
};
 
int main() {
    int input[] = {9, 31, 26, 19, 1, 13, 2, 11, 27, 16, 5, 21};
    node *hash_table[12] = {};
    // insert all elements into hash table
    for (int i = 0; i < 12; i++) {
        int index = input[i] % 12;
        if (hash_table[index] == nullptr) {
            hash_table[index] = new node({input[i], nullptr});
        } else {
            node *temp = hash_table[index];
            while (temp->next != nullptr) {
                temp = temp->next;
            }
            temp->next = new node({input[i], nullptr});
        }
    }
    return 0;
}

第八题代码:

#include <stdio.h>
 
int main() {
    int a[] = {1, 2, 4, 5, 2, 6, 3};
    int n = 7;
    int aim_index = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] < a[n - 1]) {
            aim_index++;
        }
    }
    printf("the right index is %d\n", aim_index);
}

第九题:

适用于计数排序的数据表的定义:

数据表 A 的关键码互不相同,且之间可以比较

代码:

#include <bits/stdc++.h>
 
int main() {
    int input[] = {9, 31, 26, 19, 1, 13, 2, 11, 27, 16, 5, 21};
    int dest[15] = {};
    int cc;
    for (int i = 0; i < 12; i++) {
        cc = 0;
        for (int j = 0; j < 12; j++) {
            if (input[j] < input[i]) {
                cc++;
            }
        }
        dest[cc] = input[i];
    }
    for (int i = 0; i < 12; i++) {
        std::cout << dest[i] << " ";
    }
}