Untitled
unknown
plain_text
2 years ago
3.7 kB
8
Indexable
#include <cstring>
#define MAX 50000
#define maxPOOL 250000
typedef enum
{
NAME,
NUMBER,
BIRTHDAY,
EMAIL,
MEMO
} FIELD;
int pool;
char record[MAX][5][20];
int ind;
int mark[MAX];
typedef struct
{
int count;
char str[20];
} RESULT;
struct node
{
int index;
node* next;
};
node* TB[5][MAX];
node POOL[maxPOOL];
void strCpy(char *des, char *src){
int i = 0;
while(src[i] != '\0')
{
des[i] = src[i];
i++;
}
des[i] = src[i];
}
int strCmp(char *s1, char *s2){
int i = 0;
while(s1[i] != '\0')
{
if(s1[i] != s2[i]) return s1[i] - s2[i];
i++;
}
return s1[i] - s2[i];
}
int gethash(char* str)
{
unsigned int h = 96;
int i = 0;
while(str[i])
{
h = h*31 + str[i++];
}
return h%MAX;
}
void addnewNode(int id, int h, int field)
{
node* temp = &POOL[pool++];
temp->index = id;
temp->next = nullptr;
if(TB[field][h] == nullptr)
TB[field][h] = temp;
else
{
temp->next = TB[field][h];
TB[field][h] = temp;
}
}
void addtoTB(int id)
{
for(int i=0; i<5; i++)
{
int h = gethash(record[id][i]);
addnewNode(id, h, i);
}
}
void InitDB()
{
pool = 0;
ind = 0;
memset(TB,0,sizeof(TB));
}
void Add(char* name, char* number, char* birthday, char* email, char* memo)
{
strCpy(record[ind][0], name);
strCpy(record[ind][1], number);
strCpy(record[ind][2], birthday);
strCpy(record[ind][3], email);
strCpy(record[ind][4], memo);
addtoTB(ind);
mark[ind] = 1;
ind++;
}
int Delete(FIELD field, char* str)
{
int del = 0;
int h = gethash(str);
node* temp = TB[field][h];
while(temp)
{
if(mark[temp->index] == 1 && strCmp(record[temp->index][field], str) == 0)
{
mark[temp->index] = 0;
del ++;
}
temp = temp->next;
}
return del;
}
int Change(FIELD field, char* str, FIELD changefield, char* changestr)
{
int cnt = 0;
int h = gethash(str);
node* temp = TB[field][h];
while(temp)
{
if(mark[temp->index] == 1 && strCmp(record[temp->index][field], str) == 0)
{
cnt++;
strCpy(record[temp->index][changefield], changestr);
int h1 = gethash(changestr);
addnewNode(temp->index, h1, changefield);
}
temp = temp->next;
}
return cnt;
}
RESULT Search(FIELD field, char* str, FIELD returnfield)
{
RESULT result;
result.count = 0;
int h = gethash(str);
node* temp = TB[field][h];
while(temp)
{
if(mark[temp->index] == 1 && strCmp(record[temp->index][field], str) == 0)
{
if(result.count == 0)
{
strCpy(result.str, record[temp->index][returnfield]);
}
result.count++;
}
temp = temp->next;
}
return result;
}Editor is loading...