-
Notifications
You must be signed in to change notification settings - Fork 46
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
Lyc-heng
wants to merge
2
commits into
CUCCS:master
Choose a base branch
from
Lyc-heng:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
实现了必选的排序 #171
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议将没有冲突的插入过程也输出方便查看。