嗨专家! ! ! C代码问题中的哈希表。 ... [英] Hi expert ! ! ! hash table in C code problem. ...

查看:56
本文介绍了嗨专家! ! ! C代码问题中的哈希表。 ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>

typedef struct node_type{
    int data;
    struct node_type *link;
}node;

typedef node *list;

void add_to_list(int,int);
void show();

list bucket[10];

main()
{   
    int number;
    list tet;
    char ch;
    printf("Enter y if u want to input:\n");
    tet=bucket[0];
    scanf("%c",&ch);
    while(ch=='y'){
        printf("Enter numbers\n");
        scanf("%d\n",&number);
        add_to_list(number/10,number);
        scanf("%c",&ch);
        }
    show();
}


void  add_to_list(int index,int cut){
     list q,r,temp;
     int i;
     if (bucket[index] == NULL){
              q = bucket[index] =(list)malloc(sizeof (node));
              q->data=cut;
              q->link=NULL;
}
     else{  q=bucket[index];
            while(q->link!=NULL) q=q->link;
            if(q->link==NULL){
            temp=(list)malloc(sizeof(node));
            temp->data=cut;
            temp->link=NULL;
            q->link=temp;
}
}
}
/*To show every element in the table*/

void show(){

         int i;
         list pemp;
         for(i=0;i<10;i++){
             pemp= (list)malloc(sizeof(node));
             pemp=bucket[i];
             while(pemp!=NULL){
                      printf("%d ",pemp->data);
                      pemp=pemp->link;}
}
}





什么我试过了:



有谁能告诉我我能在这做什么?问题是,当我给出相同的输入两次或两次以上时,程序崩溃了。但当我得到两个不同的值或甚至更多的值时,该程序运行良好......



What I have tried:

Could anyone please suggest me what can I do here? The problem is when I gave same input for twice or, more than twice, the program crashed. But when I got two distinct value or even more value the program worked well......

推荐答案

当我在代码块上编译代码时,它崩溃了!!!



"When I compiled the code on code blocks, it crashed!!!"

q=bucket[index];
if(q==NULL){
    q->data=cut;
    q->link=NULL;





是的。那会崩溃。试试这个。





Yup. That will crash. Try this.

if (bucket[index] == NULL)
{
    q = bucket[index] = (struct node *)malloc(sizeof node);
    q->data=cut;
    q->link=NULL;





这应该让你入门。



This should get you started.


帮个忙忙出你的缩进!

如果不出意外,它会让你明白问题是什么!

Do yourself a favour and sort out your indentation!
If nothing else, it would make it obvious to you what the problem is!
main()
{   int number,index;
    list tet;
    char ch;
    printf("Enter y if u want to input:\n");
    tet=bucket[0];
    scanf("%c",&ch);
    while(ch=='y'){
        printf("Enter numbers\n");
        scanf("%d\n",&number);
        add_to_list(number/10,number);
        scanf("%c",&ch);
    }
    show();
}
} // <<<--- What is this closing?

格式化:

Formatted:

main(){
    int number,index;
    list tet;
    char ch;
    printf("Enter y if u want to input:\n");
    tet=bucket[0];
    scanf("%c",&ch);
    while(ch=='y'){
        printf("Enter numbers\n");
        scanf("%d\n",&number);
        add_to_list(number/10,number);
        scanf("%c",&ch);
    }
    show();
}
}  // <<<--- It's obvious that is spurious.



或者更好,不要使用1TB


Or better, don't use 1TB

main()
    {
    int number,index;
    list tet;
    char ch;
    printf("Enter y if u want to input:\n");
    tet=bucket[0];
    scanf("%c",&ch);
    while(ch=='y')
        {
        printf("Enter numbers\n");
        scanf("%d\n",&number);
        add_to_list(number/10,number);
        scanf("%c",&ch);
        }
    show();
    }
}  // <<<---- Still very obvious.



你的其他代码在隐藏东西时更好......:笑:


Your other code is even better at hiding things...:laugh:


我认为 main < c> c> / code>。



您应该为程序员使用编辑器,例如 UltraEdit notepad ++ 或任何其他。

这些编辑器被称为程序员编辑器,因为它们具有允许语言突出显示和自动缩进或重新缩进的功能。
I think there is one } too many at the end of main.

You should use an editor for programmer like UltraEdit or notepad++ or any other.
these editors are said "programmer editor" because they have feature that allow language highlight and automatic indentation or re-indentation.


这篇关于嗨专家! ! ! C代码问题中的哈希表。 ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆