简易散列表实现电话号码查询系统

问题描述:按电话号码和用户名为关键字,建立散列表。实现查找用户和显示用户信息。本案例在vs2012编译通过。


1.头文件 Hsah.h

#pragma once
#include<iostream>
using namespace std;
struct Node
{
char *name; //姓名
char *address;//地址
char *num;//电话
Node *pNext;
};

class Hsah
{
private:
Node *phone;//指针存储散列表,链地址法
Node *nam;


public:
Hsah();
~Hsah(); 
int hashphone(char * num); //电话号码散列表函数
int hashname(char * name); //姓名的散列表函数
Node * push(char *pname,char *add,char *pnum);
void append(char *pname,char *add,char *pnum);//添加记录
Node * findnum(char *pnum);//按号码查找
Node * findname(char *pname);//按姓名查找
void showphone();
void showname();
};


2.Hsah.cpp文件

#include "Hsah.h"

Hsah::Hsah()
{
this->phone=new Node [20]; //分配数组空间存储哈希表
this->nam=new Node [20];
for(int i=0;i<20;i++)   // 将指针滞空
{
this->phone[i].pNext=nullptr;
this->nam[i].pNext=nullptr;
}
}

Hsah::~Hsah()
{
delete [] phone;
delete [] nam;
}

int Hsah::hashphone(char * num) //电话号码散列表函数
{
if(num==nullptr)
{
}
int key=0;
while(*num!='\0')
{
key+=(*num)-48 ;
num++;
}

return key%20;


int Hsah::hashname(char * name) //姓名的散列表函数
{
int key=0;
int i=0;
while(name[i]!=NULL)
{
key+=int(name[i]);
i++;
}
return abs(key%20); //abs函数保证key为正值
}
Node * Hsah::push(char *pname,char *add,char *pnum)//分配存储空间
{
Node *p=new Node;
    int len1 = strlen(pname);
int len2 = strlen(add);
int len3 = strlen(pnum);
p->name=new char[len1+1];
strcpy(p->name,pname);
(p->name)[len1] = '\0';
p->address = new char[len2+1];
strcpy(p->address,add);
(p->address)[len2] = '\0';
p->num=new char[len3+1];
strcpy(p->num,pnum);
(p->num)[len3] = '\0';
p->pNext=nullptr;


    return p;
}

void Hsah::append(char *pname,char *add,char *pnum)
{

Node * newphone=new Node;
Node * newname=new Node;
newphone=push(pname,add,pnum);
newname=push(pname,add,pnum);
newphone->pNext=nullptr;
newname->pNext=nullptr;

if(phone[hashphone(pnum)].pNext==nullptr)
{
newphone->pNext=phone[hashphone(pnum)].pNext;
    phone[hashphone(pnum)].pNext=newphone;
}
else
{
Node *p=phone[hashphone(pnum)].pNext;
while(p->pNext!=nullptr)
{
p=p->pNext;
}
p->pNext=newphone;
}

if(nam[hashname(pname)].pNext==nullptr)
{
newname->pNext=nam[hashname(pname)].pNext;
nam[hashname(pname)].pNext= newname;
}
else
{
Node *p=nam[hashname(pname)].pNext;
while(p->pNext!=nullptr)
{
p=p->pNext;
}
p->pNext=newname;
}  
}

Node * Hsah::findnum(char *pnum)//按号码查找
{
Node *p=phone[hashphone(pnum)].pNext;
while(p!=nullptr)
{
if(strcmp(pnum,p->num)==0)
break;
p=p->pNext;

if(p)
{
cout<<p->name<<" "<<p->num<<" "<<p->address<<endl;
}
else
{
cout<<"没有此人记录!"<<endl;
return nullptr;
}


return p;
}

Node * Hsah::findname(char *pname)//按姓名查找
{
Node *p=nam[hashname(pname)].pNext;
while(p!=nullptr)
{
if(strcmp(pname,p->name)==0)
break;
p=p->pNext;
}
if(p)
{
cout<<p->name<<" "<<p->num<<" "<<p->address<<endl;
}
else
{
cout<<"没有此人记录!"<<endl;
return nullptr;
}
return p;
}

void  Hsah::showphone()
{
for(int i=0;i<20;i++)
{
if(phone[i].pNext==nullptr)
{
continue ;
}
else
{
Node *p=phone[i].pNext; //还是phone[i].pNext部位空
while(p)
{


cout<<"姓名:"<<p->name<<endl;
cout<<"电话号码:"<<p->num<<endl;
cout<<"住宅地址:"<<p->address<<endl;


p = p->pNext;
}
}
}

}
 
void  Hsah::showname()
{
for(int i=0;i<20;i++)
{
if(nam[i].pNext==nullptr)
{
continue ;
}
else
{

Node *p=nam[i].pNext; //还是nam[i].pNext部位空
while(p)
{
cout<<"姓名:"<<p->name<<endl;
cout<<"电话号码:"<<p->num<<endl;
cout<<"住宅地址:"<<p->address<<endl;
p = p->pNext;
}

}

}
cout<<"请按任意键继续:";
cin.get();
}


3.main函数测试

#include"Hsah.h"
int main()
{
Hsah hs;
  // 添加记录
hs.append("小东","中关村22号","15675650786");
hs.append("小智","下家村58号","15685638912");
        hs.append("小明","详细28号","14226048511") ;
hs.append("小南","月月光45号","13226856411") ;
        hs.append("小北","明朗小8号","13205501111") ;
  
cout<<endl;
//按号码查找
hs.findnum("15675650786");
hs.findnum("15685638912");

hs.findnum("17584511");
cout<<endl;

//按姓名查找
hs.findname("小明");
hs.findname("小南");

hs.findname("小");

cout<<endl;


  //显示列表
hs.showphone();
cout<<endl;
hs.showname();
 
system("pause");
return 0;
}

运行结果:


注:该博客为本人第一次书写,还有很多不足,后续将一一补齐,如有侵犯到某人的权利,请联系本人,本人立马删除!

阅读更多

没有更多推荐了,返回首页