散列文件的插入、删除、查找和打印(C语言源代码)
来源:互联网 发布:php curl 选项 编辑:程序博客网 时间:2024/06/07 04:00
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #define N 13
- typedef int KeyType;
- struct ElemType
- {
- KeyType key;
- char rest[10];
- };
- struct FLNode
- {
- struct ElemType data;
- int next;
- };
- const int b1 = sizeof(KeyType);
- const int b2 = sizeof(struct FLNode);
- void InitHashFile(char *fname)
- {
- int i;
- int *A;
-
- FILE *fp;
- fp = fopen(fname, "wb+");
- if(NULL == fp)
- {
- printf("Cannot open this file!/n");
- exit(1);
- }
-
- A = (int *)calloc(N + 1, b1);
- if(NULL == A)
- {
- printf("Memory alloction failare!/n");
- exit(1);
- }
-
-
- for(i = 0; i < N + 1; i++)
- {
- A[i] = -1;
- }
-
- fwrite((char *)A, (N + 1)*b1, 1, fp);
-
- free(A);
-
- fclose(fp);
- }
- void existHash(char *fname)
- {
- char bcreate;
- char filename[128];
- FILE *fp;
- fp = fopen(fname, "rb");
- if(NULL == fp)
- {
- printf("--- 散列文件不存在, 是否新建散列文件(y:从新建立/n:打开其他散列文件)? ---/n");
- scanf("%c", &bcreate);
- if('y' == bcreate || 'Y' == bcreate)
- {
- InitHashFile(fname);
- printf("--- 新建散列文件完毕! ---/n");
- }
- else
- {
- printf("请输入散列文件路径:/n");
- scanf("%s", filename);
- strcpy(fname, filename);
- existHash(fname);
- }
- }
- else
- {
- fclose(fp);
- }
- }
- void HFInsertOne(char *fname, struct ElemType x)
- {
- int p;
- int len;
- int *A;
- int d;
- struct FLNode temp;
- struct FLNode pn;
-
- FILE *fp;
- fp = fopen(fname, "rb+");
- if(NULL == fp)
- {
- printf("Cannot open this file!/n");
- exit(1);
- }
-
- A = (int *)calloc(N + 1, b1);
- if(!A)
- {
- printf("Memory alloction failare!/n");
- exit(1);
- }
-
- fread((char *)A, (N + 1) * b1, 1, fp);
-
- d = x.key % N;
-
- temp.data = x;
- temp.next = A[d];
-
-
- if(-1 == A[N])
- {
-
- fseek(fp, 0L, 2);
-
- len = (ftell(fp) - (N+1)*b1)/b2;
-
- fwrite((char *)&temp, b2, 1, fp);
-
- A[d] = len;
- }
- else
- {
-
- p = A[N];
-
- fseek(fp, b1 * (N+1) + p*b2, 0);
- fread((char *)&pn, b2, 1, fp);
- A[N] = pn.next;
-
- fseek(fp, -b2, 1);
- fwrite((char *)&temp, b2, 1, fp);
-
- A[d] = p;
- }
-
- fseek(fp,0L,0);
- fwrite((char *)A, b1 * (N+1), 1, fp);
-
- free(A);
- fclose(fp);
- }
- int HFDelete(char *fname, struct ElemType *x)
- {
- struct FLNode tp,tq;
- int p, q;
- int *A;
- int d;
- FILE *fp;
-
- fp = fopen(fname, "rb+");
- if(NULL == fp)
- {
- printf("Cannot open this file!/n");
- exit(1);
- }
-
- A = (int *)calloc(N+1, b1);
- if(NULL == A)
- {
- printf("Memory alloction failare!/n");
- exit(1);
- }
-
- fread((char *)A, (N+1)*b1, 1, fp);
-
-
- d = x->key % N;
- p= A[d];
- while(-1 != p)
- {
- fseek(fp, (N+1)*b1+p*b2, 0);
- fread((char*)&tp, b2, 1, fp);
- if(tp.data.key == x->key)
- {
-
- *x = tp.data;
-
- if(p == A[d])
- {
- A[d] = tp.next;
- }
- else
- {
- tq.next = tp.next;
- fseek(fp, (N+1)*b1+q*b2, 0);
- fwrite((char *)&tq, b2, 1, fp);
- }
-
- tp.next = A[N];
- fseek(fp, (N+1)*b1+p*b2, 0);
- fwrite((char *)&tp, b2, 1,fp);
- A[N] = p;
-
- break;
- }
- else
- {
-
- q = p;
- tq = tp;
-
- p = tp.next;
- }
- }
-
- fseek(fp, 0L, 0);
- fwrite((char *)A, (N + 1) * b1, 1, fp);
-
- free(A);
-
- fclose(fp);
- if(-1 == p)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- int HFSearch(char *fname, struct ElemType *x)
- {
- int d;
- int p;
- int *A;
- struct FLNode temp;
-
- FILE *fp;
- fp = fopen(fname, "rb+");
- if(NULL == fp)
- {
- printf("Cannot open this file!/n");
- exit(1);
- }
-
- A = (int *)calloc(N+1, b1);
- if(NULL == A)
- {
- printf("Momery alloction failare!/n");
- exit(1);
- }
- fread((char *)A, (N+1)*b1, 1, fp);
- d = x->key % N;
-
- p = A[d];
-
- while(p != -1)
- {
- fseek(fp, (N+1)*b1 + p*b2, 0);
- fread((char *)&temp, b2, 1, fp);
- if(temp.data.key == x->key)
- {
- *x = temp.data;
- break;
- }
- else
- {
- p = temp.next;
- }
- }
-
- free(A);
-
- fclose(fp);
- if(-1 == p)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- void HFPrint(char *fname)
- {
- int i;
- int p;
- int *A;
- struct FLNode pn;
-
- FILE *fp;
- fp = fopen(fname, "rb+");
- if(NULL == fp)
- {
- printf("Cannot open this file!/n");
- exit(1);
- }
-
-
- A = (int *)calloc(N+1, b1);
- if(NULL == A)
- {
- printf("Momery alloction failare!/n");
- exit(1);
- }
- fread((char *)A, (N+1)*b1, 1, fp);
- for(i = 0; i < N+1; i++)
- {
- printf("%d:", i);
- p = A[i];
- while(-1 != p)
- {
- fseek(fp, (N+1)*b1 + p*b2, 0);
- fread((char *)&pn, b2, 1, fp);
- printf("%d->%d ", p, pn.data.key);
- p = pn.next;
- }
- printf("/n");
- }
-
-
- free(A);
-
- fclose(fp);
- }
- void main()
- {
- struct ElemType x;
- int number;
- char ch;
-
- int tag;
-
- char filename[128];
- strcpy(filename, "Hash");
-
- existHash(filename);
- while(1)
- {
- printf("/n");
- printf("/03-----------------------------------/03/n");
- printf("/03------------ 散列文件 -------------/03/n");
- printf("/03 1---- 初始化散列文件 -------------/03/n");
- printf("/03 2---- 向散列文件中插入一个元素 ---/03/n");
- printf("/03 3---- 从散列文件中删除一个元素 ---/03/n");
- printf("/03 4---- 从散列文件中查找一个元素 ---/03/n");
- printf("/03 5---- 打印散列文件 ---------------/03/n");
- printf("/03 0---- 结束运行 -------------------/03/n");
-
- printf("/03 请输入你的选择(1-5):");
- scanf("%d", &number);
- switch(number)
- {
- case 0:
- return;
- case 1:
-
- printf("确实要重新初始化散列文件(y/n)?");
- getchar();
- scanf("%c", &ch);
- if('y' == ch || 'Y' == ch)
- {
-
- InitHashFile(filename);
- printf("--- 重新初始化散列文件完毕! ---/n");
- }
- else
- {
- printf("--- 未执行重建散列文件操作! ---/n");
- }
- break;
- case 2:
-
- printf("输入待插入元素x的值(一个整数和一个字符串(长度小于10)):/n");
- scanf("%d", &x.key);
- scanf("%s", &x.rest);
- HFInsertOne(filename, x);
- break;
- case 3:
-
- printf("输入待删除元素x的关键字:");
- scanf("%d", &x.key);
- tag = HFDelete(filename, &x);
- if(1 == tag)
- {
- printf("--- 删除成功!%d %s ---/n", x.key, x.rest);
- }
- else
- {
- printf("--- 删除失败 ---/n");
- }
- break;
- case 4:
-
- printf("输入待查找元素x的关键字:/n");
- scanf("%d", &x.key);
- tag = HFSearch(filename, &x);
- if(1 == tag)
- {
- printf("--- 查找成功!%d %s ---/n", x.key, x.rest);
- }
- else
- {
- printf("查找失败!/n");
- }
- break;
- case 5:
-
- HFPrint(filename);
- break;
- default:
- printf("/n--- 输入功能号表错误 ---/n");
- break;
- }
- printf("--- 按任意键继续 ---/n");
- getch();
- }
- }