#include <iostream>
#include <cstring>
using namespace std;
int a[15];
bool find(int x)
{
if (a[x] == -1) return 0;
return 1;
}
void init()
{
memset(a, -1, sizeof(a));
}
int main()
{
init();
int n;
while (cin >> n) {
if (n == 0) break;
int x = n % 13;
if (!find(x)) {
a[x] = n;
}
else {
while (find(x)) {
x = (x + 1) % 15;
}
a[x] = n;
}
}
for (int i = 0; i < 15; i++) {
cout << i << " " << a[i] << endl;
}
return 0;
}
二.开散列表
#include <iostream>
#include <cstring>
using namespace std;
typedef struct sa {
int x;
struct sa* next;
}Hnode, * Hlist;
Hnode* H[13];
void init()
{
for (int i = 0; i < 13; i++) {
H[i] = new Hnode;
H[i]->next = NULL;
}
}
int main()
{
init();
int x;
while (cin >> x) {
if (x == 0) break;
int z = x % 13;
Hlist p;
p = new Hnode;
p->x = x;
p->next = H[z]->next;
H[z]->next = p;
}
for (int i = 0; i < 13; i++) {
cout << i << ":";
while (H[i]->next != NULL) {
cout << H[i]->next->x<<" ";
H[i] = H[i]->next;
}
cout << endl;
}
return 0;
}
三.闭散列表实现
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e4 + 5;
int a[N];
const int max1 = 1e4;
int b[N], prime[N];
int cnt;
void init()
{
memset(b, 1, sizeof(b));
b[0] = b[1] = 0;
for (int i = 2; i <= max1; i++) {
if (b[i])
prime[++cnt] = i;
for (int j = 1; j <= cnt && prime[j] * i <= max1; j++)//质数的质数倍,保证不做重复运算
{
b[prime[j] * i] = 0;//标记为0,即为非质数
if (i % prime[j] == 0) break;//解释为i的最小素因子是prime[j],但我猜测此时i==prime[j]
}
}
}
bool find(int x)
{
if (a[x] == 0) return 0;
return 1;
}
int main()
{
int m;
cin >> m;
init();
int p;
for (int i = m; i >= 2; i--) {
if (b[i]) {
p = i;
break;
}
}
int x;
while (cin >> x) {
if (x == 0) break;
int k = x % p;
if (!find(k)) a[k] = x;
else {
while (find(k)) {
k = (k + 1) % m;
}
a[k] = x;
}
}
for (int i = 0; i < m; i++) {
if (i != m - 1) printf("%4d", i);
else printf("%4d\n", i);
}
for (int i = 0; i < m; i++) {
if (i != m - 1) printf("%4d", a[i]);
else printf("%4d\n", a[i]);
}
return 0;
}
四.二叉排序树插入、删除
#include <iostream>
using namespace std;
typedef struct BSTNode {
int key;
int other;
struct BSTNode* lchild, * rchild;
}BSTnode, * BSTree;
void insert(BSTNode*& T, int key)
{
if (T == NULL) {
T = new BSTNode;
T->key = key;
T->lchild = T->rchild = NULL;
}
else {
if (T->key == key) return;
if (key < T->key) insert(T->lchild, key);
else insert(T->rchild, key);
}
}
void bianli(BSTNode* T)
{
if (T) {
bianli(T->lchild);
cout << T->key << " ";
bianli(T->rchild);
}
}
bool search(BSTNode* T, int key)
{
BSTNode* p = T;
while (p) {
if (p->key == key) return 1;
if (p->key < key) {
p = p->rchild;
}
else {
p = p->lchild;
}
}
return 0;
}
void del(BSTree& T, int key)
{
BSTree p, f;
p = T;
f = NULL;
while (p) {
if (p->key == key) break;
f = p;
if (p->key > key) p = p->lchild;
else p = p->rchild;
}
if (p == NULL) return;
BSTree q;
q = p;
if (p->lchild && p->rchild) {
BSTree s = p->lchild;
while (s->rchild) {
q = s;
s = s->rchild;
}
p->key = s->key;
if (q != p) q->rchild = s->lchild;
else q->lchild = s->lchild;
delete s;
return;
}
if (!p->rchild) p = p->lchild;
else p = p->rchild;
if (!f) T = p;
else if (f->lchild == q) f->lchild = p;
else f->rchild = p;
delete q;
}
int main()
{
BSTree T = NULL;
int x;
while (cin >> x) {
if (x == 0) break;
insert(T, x);
}
bianli(T); cout << endl;
cin >> x;
if (search(T, x)) cout << "该数存在\n";
else cout << "该数不存在\n";
cin >> x;
del(T, x);
bianli(T);
cout << endl;
return 0;
}
五.有序表折半查找算法
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int a[N];
int main()
{
int x;
int cnt = 0;
while (cin >> x) {
if (x == 0) break;
a[++cnt] = x;
}
sort(a + 1, a + 1 + cnt);
cin >> x;//1 2 3 4 5 6 7 8 9
int l = 1, r = cnt;
int mid = l + r >> 1;
int tmp = 1;
while (a[mid] != x && l != r) {
if (a[mid] < x) {
l = mid + 1;
tmp++;
}
else {
r = mid - 1;
tmp++;
}
mid = l + r >> 1;
}
if (a[mid] == x) {
cout << mid << " " << tmp << endl;
}
else {
cout << 0 << " " << tmp << endl;
}
return 0;
}