更新 'ex7_1.c'

This commit is contained in:
xtaodada 2022-05-26 14:38:08 +00:00
parent 23c8f197fc
commit eeac176312

224
ex7_1.c
View File

@ -1,78 +1,146 @@
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
#include<conio.h> #include<conio.h>
#define MAX 100 #define MAX 100
void Search1(int a[], int n, int key){ void Search1(int a[], int n, int key){
int i=0; int i=0;
a[n] = key; a[n] = key;
while(a[i] != key){ while(a[i] != key){
i++; i++;
} }
if(i == n){ if(i == n){
printf("\n未找到。\n\n"); printf("\n未找到。\n\n");
} else { } else {
printf("\n此数在第 %d 位。\n\n", i + 1); printf("\n此数在第 %d 位。\n\n", i + 1);
} }
} }
void Search2(int a[], int n, int key){ void Search2(int a[], int n, int key){
int low = 0, high = n-1, mid; int low = 0, high = n-1, mid;
while(low <= high){ while(low <= high){
mid = (low + high) / 2; mid = (low + high) / 2;
if(a[mid] == key){ if(a[mid] == key){
printf("\n此数在第 %d 位。\n\n", mid + 1); printf("\n此数在第 %d 位。\n\n", mid + 1);
return; return;
} else if (a[mid] > key){ } else if (a[mid] > key){
high = mid - 1; high = mid - 1;
} else { } else {
low = mid + 1; low = mid + 1;
} }
} }
printf("\n未找到。\n\n"); printf("\n未找到。\n\n");
} }
int main(){ void InitHashList(int b[]){
int n, choice, e, a[MAX]; int i;
for (i = 0; i < MAX; i++)
for(;;){ b[i] = 0;
printf("\t\t1.无序线性表的顺序查找\n"); }
printf("\t\t2.有序线性表的二分查找\n"); int Hash(int key){
printf("\t\t0.退出\n"); return (key * 3) % 7;
}
printf("请选择:"); int Insert(int a[], int b[], int e){
scanf("%d",&choice); int d = Hash(e);
if(choice==0) break; int temp = d;
switch (choice) { while (b[d] != 0){
case 1: d = (d + 1) % 7;
printf("请输入需要输入的元素个数:"); if (d == temp){
scanf("%d",&n); printf("散列表已满\n");
for(int i=0;i<n;i++){ return 0;
printf("请输入第 %d 个元素:",i+1); }
scanf("%d",&e); }
a[i] = e; a[d] = e;
} b[d] = 1;
printf("\n请输入需要查找的元素:"); return 1;
scanf("%d",&e); }
Search1(a, n, e); int Search3(int a[], int b[], int key){
break; int d = Hash(key);
case 2: int temp = d;
printf("请输入需要输入的元素个数:"); int p = b[d];
scanf("%d",&n); while (p != 0){
for(int i=0;i<n;i++){ if (a[d] == key){
printf("请输入第 %d 个元素:",i+1); return d;
scanf("%d",&e); } else {
a[i] = e; d = (d + 1) % 7;
} p = b[d];
printf("\n请输入需要查找的元素:"); }
scanf("%d",&e); if(temp == d){
Search1(a, n, e); break;
break; }
default: }
break; return -1;
} }
printf("请按任意键继续..."); void start_hash(int a[], int b[]){
getch(); int n, e;
system("cls"); InitHashList(a);
} InitHashList(b);
printf("感谢使用,再见\n"); printf("请输入需要输入的元素个数:");
return 0; scanf("%d",&n);
} for(int i=0; i<n; i++){
printf("请输入第 %d 个元素:", i+1);
scanf("%d", &e);
Insert(a, b, e);
}
printf("\n散列表为:");
for(int i=0; i<7; i++){
printf("%d ", a[i]);
}
printf("\n");
printf("\n请输入需要查找的元素:");
scanf("%d", &e);
n = Search3(a, b, e);
if(n != -1){
printf("\n此数在第 %d 位。\n\n", n + 1);
} else {
printf("\n未找到。\n\n");
}
}
int main(){
int n, choice, e, a[MAX], b[MAX];
for(;;){
printf("\t\t1.无序线性表的顺序查找\n");
printf("\t\t2.有序线性表的二分查找\n");
printf("\t\t3.利用散列表实现查找\n");
printf("\t\t0.退出\n");
printf("请选择:");
scanf("%d",&choice);
if(choice==0) break;
switch (choice) {
case 1:
printf("请输入需要输入的元素个数:");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("请输入第 %d 个元素:",i+1);
scanf("%d",&e);
a[i] = e;
}
printf("\n请输入需要查找的元素:");
scanf("%d",&e);
Search1(a, n, e);
break;
case 2:
printf("请输入需要输入的元素个数:");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("请输入第 %d 个元素:",i+1);
scanf("%d",&e);
a[i] = e;
}
printf("\n请输入需要查找的元素:");
scanf("%d",&e);
Search1(a, n, e);
break;
case 3:
start_hash(a, b);
break;
default:
break;
}
printf("请按任意键继续...");
getch();
system("cls");
}
printf("感谢使用,再见\n");
return 0;
}