C - Hashtable无法正常工作

时间:2017-05-23 21:20:20

标签: c hashtable

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define m 9



int flag1=1;
//Creating memory for username and password
struct node // The list that is being used to store username and password
{
    char username[30];
    char password[30];
    struct node *next;
};

int getkey(char[30]); //a method to get "randomly" a key
int hash(int);  // a method to create the hashcode
void insert(struct node **,char[30],char[30]); //adding a node struct to a specific possition in hashtable
void initializeHashTable(struct node **);
void search(struct node **,char[30]);//check if password exists
void searchuser(struct node **,char[30]); //check if username exists
void print(struct node**);//printing the hashtable

void print(struct node **T)
{
     struct node *tmp;
     int i,flag=0;
     printf("-------Data Base-------\n");
     for(i=0;i<m;i++)
     {
         tmp=*(T+i);
         while(tmp!=NULL)
         {
             printf("Username: %s  Password: %s\n",tmp->username,tmp->password);
             tmp=tmp->next;
         }

     }
}
void search(struct node **T,char password[30])
{
    struct node *tmp;
    int i,flag=0;
    for(i=0; i<m; i++)
    {
        tmp=*(T+i);
        while(tmp!=NULL)
        {
            if(strcmp(tmp->password,password)==0)
            {
                flag=1;
                break;
            }
            tmp=tmp->next;

        }
        if(flag==1)
            break;

    }
    if(flag==1)
         printf("Authentication Successfull\n");
    else
       printf("Authentication Failed\n");

}

void searchuser(struct node **T,char user[30])
{
    struct node *tmp;
    int i,flag=0;
    for(i=0; i<m; i++)
    {
        tmp=*(T+i);
        while(tmp!=NULL)
        {
            if(strcmp(tmp->username,user)==0)
            {
                flag=1;
                break;
            }
            tmp=tmp->next;

        }
        if(flag==1)
            break;

    }
    if(flag==0)
      {
           printf("Username NOT Found\n");
           flag1=0;

      }
}

void initializeHashTable(struct node **T)
{
    int i;
    for (i=0; i<m; i++)
        *(T+i)=NULL;

}
int getkey(char name[30])
{


    int i=0;
    int key=0;
    for (i=0; i<15; i++)
    {
        key+=name[i];

    }
    return key;

}

int hash(int key)
{
    return key%m;
}

void insert (struct node **T,char name[30],char password[30])
{

    struct node *newnode;
    newnode=(struct node*)malloc(sizeof(struct node));
    strcpy(newnode->username,name);
    strcpy(newnode->password,password);
    int key;
    key=getkey(name);
    int hashcode;
    hashcode=hash(key);
    //printf("Key:%d\n",key);
   // printf("Hashcode:%d\n",hashcode);

    if(*T+hashcode==NULL)
    {
        (*(T+hashcode))->next=newnode;
        newnode->next=NULL;
    }
    else
    {

        newnode->next=(*(T+hashcode));
        (*(T+hashcode))=newnode;
    }



}



int main()
{//possible content of file: phill 1234
    char choice;
    struct node **T;
    struct node **head;
    head==NULL;
    T=(struct node**)malloc(m*sizeof(struct node));//creating the hashmap
    FILE *fp;
    char name[30];
    char pass[30];

    fp=fopen("passwords.txt","r");

    if (fp==NULL)
    {
        printf("File Not Found");
        return 0;
    }
    else
    {
        initializeHashTable(&(*T));
        while(!feof(fp)) // end of file
        {
            fscanf(fp,"%s %s",name,pass); //reads until first wild space,
            //one each loop, you get 1 username and 1 password from the file
            //adding them on hashmap
            insert(&(*T),name,pass);


        }
    }

    //user authentication
    do{

        printf("Enter Username:\n");
        fflush(stdin);
        scanf("%s",&name);
        searchuser(&(*T),name);
        if(flag1==1)
        {
        printf("Enter Password:\n");
        scanf("%s",&pass);
        search(&(*T),pass);

        }
        printf("Try Again? (y/n)\n");
        fflush(stdin);
        scanf("%d",&choice);
    }while(choice!='y');

    free(T);
    free(fp);
    free(head);



    }
  • 我的代码在循环中工作正常,它执行身份验证 成功,但有时它崩溃,或我给予相同 详细信息(用户名和密码)以及身份验证失败
  • 我认为其错误的部分代码:

        //user authentication
        do{
        printf("Enter Username:\n");
        fflush(stdin);
        scanf("%s",&name);
        searchuser(&(*T),name);
        if(flag1==1)
        {
        printf("Enter Password:\n");
        scanf("%s",&pass);
        search(&(*T),pass);
    
        }
        printf("Try Again? (y/n)\n");
        fflush(stdin);
        scanf("%d",&choice);
    }while(choice!='y');
    

    如果我放do..while(choice=='y')这意味着运行循环while choice=='y'它会停止if choice=='y'这有点奇怪

  

问题:第一次尝试后,身份验证器无法正常工作

非常感谢您的光临!

1 个答案:

答案 0 :(得分:2)

突然出现的几个问题:

struct node **T;
...
T=(struct node**)malloc(m*sizeof(struct node));

您是否打算让T成为struct node的序列(在这种情况下,T的类型是错误的),或者是一系列指针struct node(在这种情况下,sizeof的参数是错误的)?

如果你写了

,它会更清晰(并且更不容易出错)
T = malloc( m * sizeof *T ); 

那么只需要确定T的类型。就个人而言,我希望你能分配一个struct node的序列,这样就可以宣布

struct node *T = malloc(  m * sizeof *T );

然而,你的其余代码似乎真的假设T是一个指向struct node的指针序列,所以在那种情况下会是

struct node **T = malloc( m * sizeof *T );

这就是这个成语的美妙 - 你唯一需要改变的是T的类型。 malloc调用本身不需要更改。如果T的类型为struct node *,则sizeof *T相当于sizeof (struct node)。如果Tstruct node **,则sizeof *T相当于sizeof (struct node *)

然后是这个:

initializeHashTable(&(*T));

应该只是

intializeHashTable( T );

此外,

while(!feof(fp))

始终是错误的 - {<1}}在之后尝试读取文件末尾之后才会返回true,因此您将经常循环播放一次。您应该检查输入操作的结果:

feof

同样,论证while ( fscanf(fp,"%s %s",name,pass) == 2 ) { insert(T,name,pass); } 应该只是&(*T)

至于你的意见:

T

此处不需要printf("Enter Username:\n"); fflush(stdin); scanf("%s",&name); 调用 - 如果在输出操作之后立即进行输入操作,则表示存在刷新。

修改

我很尴尬地说我误读了fflush电话 - 由于某种原因我把它读作fflush,这意味着你要确保你的输出在调用之前被写入控制台fflush( stdout)

在输入流上调用scanf是错误的,执行此操作的行为是未定义。它不会清除任何未读输入的输入流(MSVC除外,这是因为Microsoft决定编纂现有的不良行为)。

完全失去fflush来电。

结束修改

fflush来电应该是

scanf

此处不需要scanf( "%s", name ); - &将隐式地从“{-1}}的30个元素数组”转换为“指向name的指针”。 char也是如此。

修改

如上所述,这些char调用是不安全的 - 如果您键入的字符串长于目标缓冲区,pass将很乐意将这些额外字符写入紧跟在该缓冲区之后的内存中,从而导致从损坏的数据到seg故障的任何事情。您需要确保不要读太多字符。您可以使用字段宽度说明符(例如

)执行此操作
scanf

或使用scanf

scanf( "%29s", name );  // leave at least one element for the 0 terminator

如果有空间,fgets调用将读取并将尾随换行符存储到缓冲区,因此您需要做一些额外的工作:

fgets( name, sizeof name, stdin );

如果缓冲区中没有换行符,那么您需要在下次读取之前清除输入流:

fgets

结束修改

char *newline = strchr( name, '\n' );
if ( *newline )
  *newline = 0;

您使用错误的格式说明符来读取while ( getchar() != '\n' ) ; // empty loop - 您告诉 scanf("%d",&choice); }while(choice!='y'); 期望十进制整数输入,而不是字符。 choice与格式不匹配,因此scanf实际上并未从输入流中读取它,并且'y'未更新。您应该将其更改为

scanf

还有很多其他问题,但从这些问题开始。