本文探讨了哈希表数据结构在通讯录应用程序中的具体应用和实现方式,详细分析了如何利用哈希表提高联系人查找、插入及删除等操作效率。
```cpp
#include
#include
#include
using namespace std;
#define NULL 0
unsigned int key; //用于输入/输出文件流类的变量
unsigned int key2; //key和key2分别作为电话号码和姓名的关键字
struct node {
char name[8], address[20];
char num[11];
struct node *next;
};
typedef struct node* pnode;
void hash(char num[]) {
int i = 3, key=0; //以电话号码为关键字建立哈希函数
while(num[i] != NULL) {
key += (int)num[i];
i++;
}
key %= 20;
}
void hash2(char name[]) {
int i = 1, key2 = 0; //姓名作为关键字的哈希函数
while(name[i] != NULL) {
key2 += (int)name[i];
i++;
}
key2 %= 20;
}
node* input() { //输入节点信息,建立结点,并将结点的next指针指向空
node *temp = new node;
temp->next=NULL;
cout<<输入姓名:<>temp->name;
cout<<输入地址:<>temp->address;
cout<<输入电话:<>temp->num;
return temp;
}
int apend() { //添加节点
node *newphone, *newname;
newphone = input();
hash(newphone->num);
newname = newphone;
hash2(newname->name);
if(phone[key] == NULL) phone[key]=newnode;
if(nam[key2] == NULL) nam[key2]=newnode;
newphone->next = phone[key]->next;
phone[key]->next=newphone;
newname->next = nam[key2]->next;
nam[key2]->next=newname;
return 0;
}
void create() { //新建节点
int i;
phone= new pnode[20];
for(i=0;i<20;i++) {
phone[i]=new node;
phone[i]->next=NULL;
}
}
void create2() { //新建节点
int i;
nam = new mingzi[20];
for(i=0;i<20;i++) {
nam[i] = new node;
nam[i]->next=NULL;
}
}
void list() { //显示列表
int i, j;
pnode *p;
for(j=0;j<20;j++)
if(phone[j]!=NULL)
cout<name<<\n;
for(i=0;i<20;i++) {
node *p = phone[i]->next;
while(p) {
cout << p->name << ;
p=p->next;
}
cout << \n;
}
}
void list2() { //显示列表
int i, j;
mingzi *q;
for(j=0;j<20;j++) {
if(nam[j]!=NULL)
cout<name<<\n;
}
for(i=0;i<20;i++) {
node *p = nam[i]->next;
while(p) {
cout << p->name << ;
p=p->next;
}
cout << \n;
}
}
int main() {
char num[11];
char name[8];
create();
create2();
int sel;
while(1) {
menu();
cin>>sel;
if(sel==3){
cout<<8姓名查询 <>b;
if(b==9){
cout<<请输入电话号码:<>num;
cout<<输出查找的信息:<>name;
cout<<输出查找的信息:<