#include<bits/stdc++.h>
using namespace std;
const int M=20,mod=13;
int x;
typedef struct FLNode
{
int date;
struct FLNode *next;
}Node,*NodeList;
NodeList L[M];
void Init()
{
for(int i=0;i<mod;i++)
{
L[i]=new Node;
L[i]->next=NULL;
}
}
void Insert(int x)
{
int y=x%mod;
NodeList head,p;
head=L[y];
p=new Node;
p->date=x;
p->next=head->next;
head->next=p;
}
void OutPut()
{
for(int i=0;i<mod;i++)
{
cout<<i<<":";
NodeList p=L[i]->next;
while(p!=NULL)
{
cout<<p->date<<" ";
p=p->next;
}
cout<<endl;
}
}
int seek(int x)
{
int y=x%mod;
NodeList p=L[y]->next;
int t=0;
while(p)
{
t++;
if(p->date==x)
cout<<"查找成功,在第"<<y<<"个链表的第"<<t<<"个节点\n";
return 0;
}
cout<<"查找失败\n";
return 0;
}
int Delete(int x)
{
int y=x%mod;
NodeList pre,p;
pre=L[y];
p=pre->next;
while(p)
{
if(p->date==x)
{
pre->next=p->next;
delete p;
cout<<"删除成功"<<endl;
cout<<"输出散列表:\n";
OutPut();
return 0;
}
pre=pre->next;
p=p->next;
}
cout<<"需删除节点不存在\n";
}
int main()
{
Init();
cout<<"输入若干数(输入0结束)\n";
while(cin>>x)
{
if(x==0)
break;
Insert(x);
}
cout<<"输出散列表:\n";
OutPut();
cout<<"输入要查找的元素:";
cin>>x;
seek(x);
cout<<"输入要删除的元素:";
cin>>x;
Delete(x);
cout<<"输入要插入的节点:";
cin>>x;
Insert(x);
cout<<"输出散列表:\n";
OutPut();
return 0;
}
/*
19 14 23 1 68 20 84 27 55 11 10 79 0
55
55
55
*/
散列表
最新推荐文章于 2022-11-16 17:02:30 发布