Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现了必选的排序 #171

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 2017-1/li-yc/哈希表/1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/li-yc/哈希表/2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/li-yc/哈希表/3.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions 2017-1/li-yc/哈希表/Hashtable.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "Hashtable.h"

Hashtable CreatHash(int size) {//������ϣ��
int i;
Hashtable temp;
temp.size = size;
temp.used = 0;
temp.elements = (ElemType*)malloc(size * sizeof(ElemType));
for (i = 0; i < HashLength; ++i) {
temp.elements[i].key = -1;
temp.elements[i].val = 0;
}
return temp;
}

Status CreatArray(int arr[][2]) {//���������Ҫ���������
int i;
srand(time(0));
for (i = 0; i < ArrayLenth; ++i) {
arr[i][0] = rand() % 100 + 1;
arr[i][1] = rand() % 100 + 1;
}
return OK;
}

bool isFull(Hashtable H) {//�жϹ�ϣ���Ƿ��Ѿ���ȫʹ��
return H.size == H.used;
}

Status Insert(Hashtable H, KeyType keyvalue, ValueType value) {//����ϣ���ڲ���Ԫ��
int d = 0;
int pos = (keyvalue + d) % H.size;
if (isFull(H)) {
printf("��ϣ������\n");
RebulidHash(&H);
return ERROR;
}
while (H.elements[pos].key != -1 && keyvalue != -1) {
d++;
pos = (pos + d) % H.size;
}
if (keyvalue != -1) {
H.elements[pos].key = keyvalue;
H.elements[pos].val = value;
}
if (d != 0) {
printf("\n----[����%dԪ��ʱ�����˳�ͻ%d��]----\n", keyvalue, d);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议将没有冲突的插入过程也输出方便查看。

return OK;
}

Status Find(Hashtable H, KeyType keyval) {//�ڹ�ϣ���в���Ԫ��
int d = 0;
int pos = (keyval + d) % H.size;
printf("\n�ڱ��в��ҹؼ���%d\n", keyval);
while (H.elements[pos].key != keyval) {
if (H.elements[pos].key == -1) {
printf("�����ҵĹؼ��ֲ��ڱ���\n");
return ERROR;
}
d++;
pos = (pos + d) % H.size;
}
printf("�ڹ�ϣ����λ��%d�ҵ��ؼ���%d\n\n", pos, keyval);
return OK;
}

Status RebulidHash(Hashtable *H) {//�ؽ���ϣ��
int newsize = (*H).size;
int i;
Hashtable temp = CreatHash((*H).size);
printf("\n�ؽ���ϣ��\n");
for (i = 0; i < (*H).size; ++i) {
temp.elements[i].key = (*H).elements[i].key;
temp.elements[i].val = (*H).elements[i].val;
}
(*H).size *= 2;
(*H).elements = (ElemType*)malloc((*H).size * sizeof(ElemType));
for (i = 0; i < (*H).size; ++i) {
(*H).elements[i].key = -1;
(*H).elements[i].val = 0;
}
for (i = 0; i < newsize; ++i) {
Insert(*H, temp.elements[i].key, temp.elements[i].val);
}
return OK;
}

Status TraverseHash(Hashtable H) {//������ϣ��
int i;
int flag=0;
for (i = 0; i < H.size; ++i) {
printf("{ [%d] : %d->%d } \n", i, H.elements[i].key, H.elements[i].val);
}
printf("\n");
return OK;
}
35 changes: 35 additions & 0 deletions 2017-1/li-yc/哈希表/Hashtable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ArrayLenth 10 //��������鳤��
#define HashLength 12 //��ϣ����ʼ������

typedef int KeyType;
typedef int ValueType;
typedef struct _ElemType {
KeyType key; // �ؼ���
ValueType val; // ֵ
} ElemType;
typedef struct {
int size;
int used;
ElemType* elements;
}Hashtable;
typedef enum {
false,
true,
}bool;
typedef enum {
OK,
OVERFLOW,
ERROR,
} Status;

Hashtable CreatHash(int size);
Status CreatArray(int arr[][2]);
bool isFull(Hashtable H);
Status Insert(Hashtable H, KeyType keyvalue, ValueType value);
Status Find(Hashtable H, KeyType keyval);
Status RebulidHash(Hashtable *H);
Status TraverseHash(Hashtable H);
34 changes: 34 additions & 0 deletions 2017-1/li-yc/哈希表/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "Hashtable.h"

int main() {
Hashtable H;
int arr[ArrayLenth][2];
int i;

srand(time(0));

//������ϣ��
H = CreatHash(HashLength);
CreatArray(arr);

//��ʼ����ϣ��
for (i = 0; i < ArrayLenth; ++i) {
Insert(H, arr[i][0], arr[i][1]);
}
printf("��ϣ���������\n");
TraverseHash(H);

//�ؼ��ֲ���
for (i = 0; i < HashLength / 2; ++i) {
Find(H, arr[rand() % ArrayLenth][0]);
}
for (i = 0; i < HashLength / 2; ++i) {
Find(H, rand() % 100 + 1);
}

//�ؽ���ϣ��
RebulidHash(&H);
TraverseHash(H);

return 0;
}