今天学了【链地址法处理冲突构造哈希表】,帮别人写了个程序。

来源:互联网 发布:seo招聘北京 编辑:程序博客网 时间:2024/05/01 04:43

题意大概就是  实现注册 登陆QQ号码,返回注册成功 被注册 号码不存在 密码正确 密码错误等。


原题:

 QQ Account Management  
You are supposed to implement the functions of account "Log in" and "Register" for the most popular instant messager QQ.  The most challenging part is that QQ now has more than a billion users.  
Input Specification: 
Each input file contains one test case.  For each case, the first line contains an integer N (105) - the total number of queries.  Then N lines follow, each contains a query in the format:  
Command QQ_No Password where "Command" is either "R" (meaning to register a new account, and hence followed by a new account number and a password), or "L" (meaning to log in with an existing account, and hence followed by the account number and the password); "QQ_No" is an integer that is greater than 1000 and no more than 10 digits long; and "Password" is a string with no less than 6 and no more than 16 characters without any space.  
Output Specification: For each test case, print the corresponding message for each query in a line.  The messages are:  If a new account is successfully registered, output "Register Successful";  If the new registering account number already exists, output "ERROR: Account Number Already Exists";  If log in successfully, output "Log in Successful";  If the log in account does not exist, output "ERROR: Account Not Exist";  If log in with a wrong password, output "ERROR: Wrong Password".  
Sample Input: 
5

L 1234567890 myQQ@qq.com

R 1234567890 myQQ@qq.com

R 1234567890 myQQ@qq.com

L 1234567890 myQQ@qq

L 1234567890 myQQ@qq.com  
Sample Output:  
ERROR: Account Not Exist

Register Successful

ERROR: Account Number Already Exists 

ERROR: Wrong Password

Log in Successful


代码:

#include<stdio.h>#include<stdlib.h>#include<string.h>#define Modd 43853#define debug 0struct keyNum*hash[Modd+10];struct keyNum*insertHash(struct keyNum*,int,char*);//关键字插入链表int searchHash(struct keyNum*,int m);//查找链表中是否存在关键字为m的整数char* searchPass(struct keyNum*,int m);//查询链表中QQ号m所对应的密码并返回void print(struct keyNum*);//打印链表struct keyNum{    int key;//关键字    char pass[20];    struct keyNum *next;};int main(){    int n,i,j;    int qqNum;    char password[20];    char flag;    struct keyNum *head=NULL;    while(scanf("%d",&n)!=EOF)    {        memset(hash,0,sizeof(hash));        for(i=0;i<n;i++)        {            scanf("%c",&flag);            while(flag != 'L' && flag != 'R')                scanf("%c",&flag);            if(flag=='L')         //登陆            {                scanf("%d%s",&qqNum,password);//获取关键字                int m=qqNum%Modd;//计算得到关键字的哈希值                int exist=searchHash(hash[m],qqNum);//将关键字qqNum插入到哈希值为m的链表中                if(exist)//要登陆的QQ号已存在                {                    char* correctPass=searchPass(hash[m],qqNum);//查询到的密码                    if(strcmp(correctPass,password) == 0)//登陆成功                    {                        printf("Log in Successful\n");                    }                    else//密码错误返回ERROR                    {                        printf("ERROR: Wrong Password\n");                    }                }                else//要登陆的QQ号不存在返回ERROR信息                {                    printf("ERROR: Account Not Exist\n");                }            }            else if(flag=='R')    //注册            {                scanf("%d%s",&qqNum,password);//获取关键字                int m=qqNum%Modd;//计算得到关键字的哈希值                if (debug) printf("QQ=%d  m=%d\n",qqNum,m);                int exist=searchHash(hash[m],qqNum);//将关键字qqNum插入到哈希值为m的链表中                if (debug) printf("存在=%d\n",exist);                if(exist)//要注册的QQ号已存在返回ERROR信息                {                    printf("ERROR: Account Number Already Exists\n");                }                else//要注册的QQ号不存在则注册                {                    hash[m]=insertHash(hash[m],qqNum,password);                    printf("Register Successful\n");                    if (debug)                    {                        printf("采用链地址法得到的哈希表为:\n");                        for(i=0;i<Modd;i++)                        {                            printf("第%d行:",i);                            print(hash[i]);                            printf("\n");                        }                    }                }            }        }    }    return 0;}struct keyNum *insertHash(struct keyNum*head,int m,char *password){    struct keyNum *p0,*p1,*p2,*temp;    temp=(struct keyNum*)malloc(sizeof(struct keyNum));    temp->key=m;    int i;    for(i=0;i<20;i++)    temp->pass[i]=password[i];    p1=head;    p0=temp;//要插入的节点(值为m);    if(head==NULL)//1,原来的链表为空,插入到head后    {        head=p0;        p0->next=NULL;    }    else//原来的链表不为空    {        while((p0->key>p1->key)&&(p1->next!=NULL))//移动到适当位置        {            p2=p1;            p1=p1->next;        }        if(p0->key<=p1->key)        {            if(head==p1)head=p0;//2,插入到第一个节点之前            else p2->next=p0;//3,插入到p2指向的节点之后            p0->next=p1;        }        else//4,插入到结尾处        {            p1->next=p0;            p0->next=NULL;        }    }    return(head);}int searchHash(struct keyNum*head,int m)//查找链表head中是否存在m{    int k=0;    struct keyNum*p;    p=head;    if(head!=NULL)        do        {            if(p->key==m) //存在m            {                k=1;                break;            }            p=p->next;        }while(p!=NULL);    return(k);//存在m值则返回1,否则返回0;}char* searchPass(struct keyNum*head,int m)//查询链表中QQ号m所对应的密码并返回{    char* k;    struct keyNum*p;    p=head;    if(head!=NULL)        do        {            if(p->key==m) //存在m            {                k=p->pass;                break;            }            p=p->next;        }while(p!=NULL);    return(k);//存在m值则返回1,否则返回0;}void print(struct keyNum*head)//打印链表head{    struct keyNum*p;    p=head;    if(head!=NULL)    {        do        {            printf(" -> %d ",p->key);            p=p->next;        }while(p!=NULL);    }    else        printf("null");}/*5L 1234567890 myQQ@qq.comR 1234567890 myQQ@qq.comR 1234567890 myQQ@qq.comL 1234567890 myQQ@qqL 1234567890 myQQ@qq.com*/

0 0