散列表

#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
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值