#include<iostream>
#include<fstream>
#include<malloc.h>
#include<stdlib.h>
const int ERROR=0;
using namespace std;
struct LinkHash
{
LinkHash *next;
int m_nValue;
int count;
};
struct _Data
{
int Value;
int Count;
};
char *file[101]=
{};
class CHashTable
{
private:
LinkHash *HashTable[101];
public:
CHashTable();
~CHashTable();
void HashCollision(int data);
void WriteToFile();
_Data GetMaxFreq(char *filename);
};
CHashTable::CHashTable()
{
int i;
for(i=0;i<100;i++)
{
HashTable[i]=(LinkHash*)malloc(sizeof(LinkHash));
if(!HashTable[i])
exit(ERROR);
HashTable[i]->count=0;
HashTable[i]->next=NULL;
HashTable[i]->m_nValue=-1;
}
}
CHashTable::~CHashTable()
{
}
int HashFunc(int key)
{
return key%100;
}
void CHashTable::HashCollision(int data)
{
LinkHash *newNode;
LinkHash *head;
newNode=(LinkHash*)malloc(sizeof(LinkHash));
if(!newNode)
exit(ERROR);
newNode->next=NULL;
newNode->m_nValue=data;
newNode->count=0;
int p;
bool isRep=false;
p=HashFunc(data);
head=HashTable[p];
while(head->next)
{
head=head->next;
if(head->m_nValue==data)
{
head->count++;
isRep=true;
break;
}
}
if(isRep==false)
{
head->next=newNode;
head=newNode;
head->count++;
}
}
void CHashTable::WriteToFile()
{
int i;
ofstream fout;
for(i=0;i<100;i++)
{
LinkHash *p;
fout.open(file[i]);
if(HashTable[i]->next)
{
p=HashTable[i]->next;
while(p)
{
fout<<p->m_nValue<<" "<<p->count<<endl;
p=p->next;
}
}
fout.close();
fout.clear();
}
}
汪汪一只猫