主题:散列作业啊,大家帮帮我哦。。
#include "Dic.h"
//fixed errors: nxt, not next. make DicNode members public (or do friend thing)
//made hash a private helper member function (so it could use SZ).
//made the destructor logic a separate helper method so it could be called
// from 2 different places.
//Did the copycon and operator=.
int Dic::hash(K key){ //DEPENDS ON K (must be member of Dic to use SZ)
return key % SZ;
}
void Dic::deallocate(){ //separate member called by destruc and op=
for(int i=0; i<SZ; i++){
//get rid of chain i
DicNode * p = hashTable[i];
while(p!=0){
DicNode * kill = p;
p = p->nxt;
delete kill;
}
}
delete [] hashTable;
}
//-----------------------------------------------------------------
Dic::Dic( ){ //an empty Dic
n=0;
SZ = 7;
hashTable = new DicNode*[SZ];
for(int i=0; i<SZ; i++) hashTable[i]=0;
}
//BIG 3
Dic::~Dic(){this->deallocate();}
Dic::Dic( const Dic & src ){ //copy con
*this = src; //Uses operator= defined for Dic
}
Dic & Dic::operator=( const Dic & rhs ){ //assignment op
if(this == &rhs){ cout<<"goofy"<<endl; return *this; }
// clean up any memory allocated by this
this->deallocate();
// initialize this n,SZ,hashTable to be like rhs
this->n=rhs.n; this->SZ = rhs.SZ; this->hashTable=new DicNode*[SZ];
// duplicate the DicNode chains
for(int i=0;i<SZ;i++){
DicNode * q = rhs.hashTable[i];
if(q==0){
this->hashTable[i]=0;
}else{
this->hashTable[i]=new DicNode; //note: NOT DicNode()
DicNode * p = this->hashTable[i];
while(true){ //loop inv: *p is blank node corresp to *q
p->key = q->key; p->val = q->val;
if(q->nxt==0)break;
q=q->nxt; p->nxt=new DicNode; p=p->nxt;
}
p->nxt=0;
}
}
return *this;
}
//return null or a pointer at the value in this Dic
V * Dic::find(K key){
int idx = this->hash(key);
DicNode * p = hashTable[idx];
while(p!=0 && p->key != key) p=p->nxt;
if(p==0) return 0; else return &(p->val);
}
//returns true if it ADDED (false if modified)
bool Dic::addOrMod(K key, V val){
V * p = find(key);
if(p==0){
//add this pair to this dictionary
DicNode * q = new DicNode;
q->key = key; q->val = val;
int idx = hash(key);
q->nxt = hashTable[idx]; hashTable[idx] = q;
return true;
}else{
*p = val;
return false;
}
}
int Dic::size( ){
return n;
}
这个是一个cpp
#include <iostream>
#include <string>
#include "Dic.h"//where keys are strings and values are double
using namespace std;
string genKey(int len){ //generates a random string
string s;
for(int i=0;i<len;i++) s = s + (char)('A' + rand()%26);
return s;
}
int main(){
int n; cout<<"n= "; cin>>n; //put n items into the dictionary
int len; cout<<"len= "; cin>>len; //each key is a string of length len
Dic d;
for( int item=0; item<n; item++){
string key = genKey(len);
d.addOrMod("haha",12);
d.addOrMod("billy",12.3);
d.addOrMod("john", item);
}
cout<<"size="<<d.size()<<endl;
for(int i=0; i<100; i++){
string key = genKey(len); if(i==11) key="billy";
double * x = d.find(key);
if(x) cout<< key <<": "<<*x<<endl;
else cout<< key <<" not found"<<endl;
}
cout<<"value for john was "<< *(d.find("john"))<<endl;
return 0;
}
另外一个cpp
#ifndef DIC_H
#define DIC_H
#include <iostream>
#include <string>
using namespace std;
typedef int K; //key type
typedef string V; //value type
class Dic{
public:
Dic( ); //an empty Dic
//BIG 3
~Dic();
Dic( const Dic & src ); //copy con
Dic & operator=( const Dic & rhs ); //assignment op
//return null or a pointer at the value in this Dic
V * find(K key);
//returns true if it ADDED (false if modified)
bool addOrMod(K key, V val);
int size( );
private:
class DicNode{public: K key; V val; DicNode * nxt; };
DicNode * * hashTable;
int n; int SZ;
int hash(K);
void deallocate();
};
#endif
一个.h 文件
教教我怎么改它才能运行。。。。。。
//fixed errors: nxt, not next. make DicNode members public (or do friend thing)
//made hash a private helper member function (so it could use SZ).
//made the destructor logic a separate helper method so it could be called
// from 2 different places.
//Did the copycon and operator=.
int Dic::hash(K key){ //DEPENDS ON K (must be member of Dic to use SZ)
return key % SZ;
}
void Dic::deallocate(){ //separate member called by destruc and op=
for(int i=0; i<SZ; i++){
//get rid of chain i
DicNode * p = hashTable[i];
while(p!=0){
DicNode * kill = p;
p = p->nxt;
delete kill;
}
}
delete [] hashTable;
}
//-----------------------------------------------------------------
Dic::Dic( ){ //an empty Dic
n=0;
SZ = 7;
hashTable = new DicNode*[SZ];
for(int i=0; i<SZ; i++) hashTable[i]=0;
}
//BIG 3
Dic::~Dic(){this->deallocate();}
Dic::Dic( const Dic & src ){ //copy con
*this = src; //Uses operator= defined for Dic
}
Dic & Dic::operator=( const Dic & rhs ){ //assignment op
if(this == &rhs){ cout<<"goofy"<<endl; return *this; }
// clean up any memory allocated by this
this->deallocate();
// initialize this n,SZ,hashTable to be like rhs
this->n=rhs.n; this->SZ = rhs.SZ; this->hashTable=new DicNode*[SZ];
// duplicate the DicNode chains
for(int i=0;i<SZ;i++){
DicNode * q = rhs.hashTable[i];
if(q==0){
this->hashTable[i]=0;
}else{
this->hashTable[i]=new DicNode; //note: NOT DicNode()
DicNode * p = this->hashTable[i];
while(true){ //loop inv: *p is blank node corresp to *q
p->key = q->key; p->val = q->val;
if(q->nxt==0)break;
q=q->nxt; p->nxt=new DicNode; p=p->nxt;
}
p->nxt=0;
}
}
return *this;
}
//return null or a pointer at the value in this Dic
V * Dic::find(K key){
int idx = this->hash(key);
DicNode * p = hashTable[idx];
while(p!=0 && p->key != key) p=p->nxt;
if(p==0) return 0; else return &(p->val);
}
//returns true if it ADDED (false if modified)
bool Dic::addOrMod(K key, V val){
V * p = find(key);
if(p==0){
//add this pair to this dictionary
DicNode * q = new DicNode;
q->key = key; q->val = val;
int idx = hash(key);
q->nxt = hashTable[idx]; hashTable[idx] = q;
return true;
}else{
*p = val;
return false;
}
}
int Dic::size( ){
return n;
}
这个是一个cpp
#include <iostream>
#include <string>
#include "Dic.h"//where keys are strings and values are double
using namespace std;
string genKey(int len){ //generates a random string
string s;
for(int i=0;i<len;i++) s = s + (char)('A' + rand()%26);
return s;
}
int main(){
int n; cout<<"n= "; cin>>n; //put n items into the dictionary
int len; cout<<"len= "; cin>>len; //each key is a string of length len
Dic d;
for( int item=0; item<n; item++){
string key = genKey(len);
d.addOrMod("haha",12);
d.addOrMod("billy",12.3);
d.addOrMod("john", item);
}
cout<<"size="<<d.size()<<endl;
for(int i=0; i<100; i++){
string key = genKey(len); if(i==11) key="billy";
double * x = d.find(key);
if(x) cout<< key <<": "<<*x<<endl;
else cout<< key <<" not found"<<endl;
}
cout<<"value for john was "<< *(d.find("john"))<<endl;
return 0;
}
另外一个cpp
#ifndef DIC_H
#define DIC_H
#include <iostream>
#include <string>
using namespace std;
typedef int K; //key type
typedef string V; //value type
class Dic{
public:
Dic( ); //an empty Dic
//BIG 3
~Dic();
Dic( const Dic & src ); //copy con
Dic & operator=( const Dic & rhs ); //assignment op
//return null or a pointer at the value in this Dic
V * find(K key);
//returns true if it ADDED (false if modified)
bool addOrMod(K key, V val);
int size( );
private:
class DicNode{public: K key; V val; DicNode * nxt; };
DicNode * * hashTable;
int n; int SZ;
int hash(K);
void deallocate();
};
#endif
一个.h 文件
教教我怎么改它才能运行。。。。。。