首页 > 代码库 > C++__哈希表(练习)

C++__哈希表(练习)

哈希表

 

文件结构:哈希表→DATA→DATA.h、DATA.cpp

        →LINK→LINK.h、LINK.cpp

        →HASH→HASH.h、HASH.cpp


 

data.h

#ifndef DATA_H_
#define DATA_H_

#include <iostream>
using namespace std;

class DATA {
private:
    string name;
    string passwd;
public:
    DATA(string name, string passwd);
    ~DATA();
    string getName() const;
    string getPasswd() const;
    void setName(string name);
    void setPasswd(string passwd);
};

#endif /* DATA_H_ */

 

data.cpp

/*
 * DATA.cpp
 *
 *  Created on: 2017-1-21
 *      Author: root
 */

#include "DATA.h"

DATA::DATA(string name, string passwd) {
    // TODO Auto-generated constructor stub
    this->setName(name);
    this->setPasswd(passwd);
}

DATA::~DATA() {
    // TODO Auto-generated destructor stub
}

string DATA::getName() const {
    return name;
}

string DATA::getPasswd() const {
    return passwd;
}

void DATA::setName(string name) {
    this->name = name;
}

void DATA::setPasswd(string passwd) {
    this->passwd = passwd;
}

 

link.h

#ifndef LINK_H_
#define LINK_H_

#include "../DATA/DATA.h"

class LINK {
    DATA *data;
    LINK *next;
public:
    LINK(DATA *data);
    ~LINK();
    DATA *getData() const;
    LINK *getNext() const;
    void setData(DATA *data);
    void setNext(LINK *next);
};

#endif /* LINK_H_ */

 

link.cpp

#include "LINK.h"

LINK::LINK(DATA *data) {
    // TODO Auto-generated constructor stub
    if (data)
        this->setData(data);
}

DATA *LINK::getData() const {
    return data;
}

LINK *LINK::getNext() const {
    return next;
}

void LINK::setData(DATA *data) {
    this->data =http://www.mamicode.com/ data;
}

void LINK::setNext(LINK *next) {
    this->next = next;
}

LINK::~LINK() {
    // TODO Auto-generated destructor stub
}

 

hash.h

#ifndef HASH_H_
#define HASH_H_

#include "../LINK/LINK.h"

#define HASH_SIZE 26

enum HASH_OP {
    ERR = -1, OK
};

class HASH {
private:
    LINK *hash[HASH_SIZE];
public:
    HASH();
    ~HASH();

    LINK *getHash(unsigned int n);
    void setHash(LINK *pLink, unsigned int n);

    void DestroyHash(HASH *pHash);
    int InsertItem(HASH *pHash, string Name, string Pwd);
    LINK *DeleteItem(HASH *pHash, string Name, string Pwd);
    void ShowHash(HASH *pHash);
    void SelectItem(HASH *pHash, string Name);
};

#endif /* HASH_H_ */

 

hash.cpp

  1 #include "HASH.h"
  2 
  3 HASH::HASH() {
  4     // TODO Auto-generated constructor stub
  5 }
  6 
  7 HASH::~HASH() {
  8     // TODO Auto-generated destructor stub
  9 }
 10 
 11 LINK *HASH::getHash(unsigned int n) {
 12     return hash[n];
 13 }
 14 
 15 void HASH::setHash(LINK *pLink, unsigned int n) {
 16     this->hash[n] = pLink;
 17 }
 18 
 19 int HASH::InsertItem(HASH *pHash, string Name, string Pwd) {
 20     if ((!pHash) || Name.empty() || Pwd.empty())
 21         return ERR;
 22 
 23     DATA *DATA_New = new DATA(Name, Pwd);
 24     if (!DATA_New) {
 25         cout << "new data error" << endl;
 26         return ERR;
 27     }
 28     LINK *LINK_New = new LINK(DATA_New);
 29     if (!LINK_New) {
 30         cout << "new link error" << endl;
 31         return ERR;
 32     }
 33     int iNum = Name[0] - a;
 34     LINK *Tmp = pHash->getHash(iNum);
 35     if (!Tmp) {
 36         pHash->setHash(LINK_New, iNum);
 37     } else {
 38         while (Tmp->getNext()) {
 39             if (Tmp->getData()->getName() == Name) {
 40                 cout << "The hash have the name, Insert defeated" << endl;
 41                 delete DATA_New;
 42                 delete LINK_New;
 43                 return ERR;
 44             }
 45             Tmp = Tmp->getNext();
 46         }
 47         Tmp->setNext(LINK_New);
 48     }
 49 
 50     return OK;
 51 }
 52 
 53 void HASH::DestroyHash(HASH *pHash) {
 54     if (!pHash)
 55         return;
 56 
 57     DATA *DATA_Tmp = NULL;
 58     LINK *LINK_Tmp = NULL;
 59     int i = 0;
 60     while (i < HASH_SIZE) {
 61         LINK_Tmp = pHash->getHash(i);
 62         while (LINK_Tmp) {
 63             DATA_Tmp = LINK_Tmp->getData();
 64             delete DATA_Tmp;
 65             pHash->setHash(LINK_Tmp->getNext(), i);
 66             delete LINK_Tmp;
 67             LINK_Tmp = pHash->getHash(i);
 68         }
 69         i++;
 70     }
 71     delete pHash;
 72 
 73     return;
 74 }
 75 
 76 LINK *HASH::DeleteItem(HASH *pHash, string Name, string Pwd) {
 77     if ((!pHash) || Name.empty() || Pwd.empty())
 78         return NULL;
 79 
 80     int iNum = Name[0] - a;
 81     LINK *Tmp = pHash->getHash(iNum);
 82     if (!Tmp) {
 83         cout << "name or password error" << endl;
 84         return NULL;
 85     } else if (Tmp->getData()->getName() == Name) {
 86         if (Tmp->getData()->getPasswd() == Pwd) {
 87             pHash->setHash(Tmp->getNext(), iNum);
 88             Tmp->setNext(NULL);
 89             return Tmp;
 90         }
 91         cout << "name or password error" << endl;
 92         return NULL;
 93     } else {
 94         LINK *Del = Tmp->getNext();
 95         while (Del) {
 96             if (Del->getData()->getName() == Name) {
 97                 if (Del->getData()->getPasswd() == Pwd) {
 98                     Tmp->setNext(Del->getNext());
 99                     Del->setNext(NULL);
100                     return Del;
101                 }
102                 cout << "name or password error" << endl;
103                 return NULL;
104             }
105             Tmp = Del;
106             Del = Tmp->getNext();
107         }
108     }
109 
110     return NULL;
111 }
112 
113 void HASH::ShowHash(HASH *pHash) {
114     if (!pHash)
115         return;
116 
117     int i = 0;
118     LINK *Tmp = NULL;
119 
120     while (i < HASH_SIZE) {
121         Tmp = pHash->getHash(i);
122         while (Tmp) {
123             cout << (char) (a + i) << ":" << endl;
124             cout << "Name:" << Tmp->getData()->getName() << "  "
125                     << "PassWord:******" << endl;
126             ;
127             Tmp = Tmp->getNext();
128         }
129         i++;
130     }
131 
132     return;
133 }
134 
135 void HASH::SelectItem(HASH *pHash, string Name) {
136     if ((!pHash) || Name.empty())
137         return;
138 
139     int iNum = Name[0] - a;
140     LINK *Tmp = pHash->getHash(iNum);
141     if (!Tmp) {
142         cout << "The hash not have the " << Name << endl;
143     }
144     while (Tmp) {
145         if (Tmp->getData()->getName() == Name) {
146             cout << "The hash have the " << Name << endl;
147             break;
148         }
149         Tmp = Tmp->getNext();
150     }
151 
152     return;
153 }

 

main.cpp

#include "HASH/HASH.h"
#include <iostream>
using namespace std;

void function() {
    cout << "sizeof(HASH) = " << sizeof(HASH) << endl;
    cout << "sizeof(LINK) = " << sizeof(LINK) << endl;
    cout << "sizeof(DATA) = " << sizeof(DATA) << endl;
    HASH *pHash = new HASH;
    if (!pHash) {
        cout << "new hash error" << endl;
        return;
    }
    string name[5]; // = new string[5];
    string pwd[5]; // = new string[5];
    name[0] = "mei zu";
    pwd[0] = "note";
    name[1] = "xiao mi";
    pwd[1] = "note";
    name[2] = "hua wei";
    pwd[2] = "honor";
    name[3] = "oppo";
    pwd[3] = "s plus";
    name[4] = "vivo";
    pwd[4] = "x plus";
    int i = 0;
    while (i < 5) {
        pHash->InsertItem(pHash, name[i], pwd[i]);
        i++;
    }
//    pHash->ShowHash(pHash);

    pHash->DeleteItem(pHash, "vivo", "x plus");
//    pHash->ShowHash(pHash);

    pHash->InsertItem(pHash, "iPhone", "plus");

    pHash->DeleteItem(pHash, "oppo", "x plus");
    pHash->ShowHash(pHash);

    pHash->SelectItem(pHash, "iPhone");

    pHash->SelectItem(pHash, "luk");

    pHash->DestroyHash(pHash);
    cout << "end" << endl;
}

int main() {
    function();
    return 0;
}

 

C++__哈希表(练习)