#include<stdio.h>
#include<stdlib.h>
#define NUM 5
typedef struct List {
int num;
char* data;
}List;
List* initList() {
List* L = (List*)malloc(sizeof(List));
L->num = 0;
L->data = (char*)malloc(sizeof(char) * NUM);
for (int i = 0; i < NUM; i++)
{
L->data[i] = 0;
}
return L;
}
int hush(int data) {
return data % NUM;
}
void put(List*L,int data) {
int index = hush(data);
int count = 1;
if (L->data[index]!=0)
{
index = hush(hush(data) + count);
//一直找下去,且用外层的hush保证不越界
count++;
}
L->data[index] = data;
L->num++;
}
int main()
{
List* list = initList();
put(list, 'A');
put(list, 'F');
printf("%c\n", list->data[0]);
printf("%c\n", list->data[1]);
return 0;
}
参考: