【问题标题】:array of linked list for hash table哈希表的链表数组
【发布时间】:2018-12-27 01:48:13
【问题描述】:

我想知道带 10 个名字的文本文件并阅读它。 10个名字按降序排序,用除法形成哈希表。我需要构建它们的链表。哈希表的索引为 7。

我已经尝试过匹配指针变量并制作了一个哈希表,但我做不到。我在制作哈希表、插入数据、打印哈希表和搜索数据时遇到了麻烦(当我键入名称时要查找的函数。)。我需要添加更多功能..我是怎么做到的?

#define SIZE 7


struct node {
   char data[100][20];
   struct node* next; 
}; 

struct index {
   struct node* head; 
   int count; 
};



struct sum (data){ 
   struct node* ptr;

   int sum,i;
   for (i=0; i<20; i++) {
    ptr -> data[i] = ptr;
    strcpy(sum,ptr);
   }

  return sum;   
};


int hashFunction (int sum) { 

    return sum%SIZE; 
}



void descend (data) { 

    int temp;
    for(i=0;i<100;i++) {
       for(j=0;j=20;j++) {
         if (data[i][j+1]>data[i][j])
            temp=data[i][j];
            data[i][j]=data[i][j+1];
            data[i][j+1]=temp;
         }
    }   
}



int main (void) {

    char data[100][20];
    FILE *fp;
    fp = fopen("data.txt","r");
    for (int i=0; i<20; i++)
        fscanf (fp,"%s",&data);
        printf("%s\n",data);
    }
    fclose(fp);
    hashTable = (struct index*)malloc(SIZE*sizeof(struct index));
    descend(data);
    return 0;
}

【问题讨论】:

  • fscanf (fp,"%s",&amp;data)不应该是fscanf (fp,"%s",&amp;data[i]),因为data是二维数组。
  • 该代码中有很多错误,请编译并询问所有警告并查看编译器产生的错误和警告以修复它们

标签: c


【解决方案1】:

代码中有很多错误,我只是把我可能的观察。首先这个

fscanf (fp,"%s",&data);

应该是

fscanf (fp,"%s",&data[i]);

其次,在descend() 函数内部循环条件部分,您使用的是j=20,它会无限循环运行。这是 MACRO 派上用场的地方,因为这个 j=20 只是运行,即如果它可以有 ROW=j 其中 ROW20 编译器会产生有意义的错误。这个

void descend (data) { /* what is the tyep of data ?  you should mention the data type */ 
    int temp;
    for(i=0;i<100;i++) { /* there are only 20 lines not 100 i.e it should be i<20 */
       for(j=0;j=20;j++) { /* condition is wrong, you indented for j<20 but that too
                             wrong as there are supposed to be max 100 char in line 
                             it should be j<100 */
         if (data[i][j+1]>data[i][j]) /* condition is not correct */
            temp=data[i][j];
            data[i][j]=data[i][j+1];
            data[i][j+1]=temp;
         }
    }   
}

正确版本descend函数可以

void descend (char (*data)[ROW], int col) {  /* define ROW as macro with value 20 and pass the col i.e 100 */

    int temp;
    for(i=0;i < ROW; i++) {
       for(j=0;j < col; j++) {
         if (data[i][j] > data[i][j+1])
            temp = data[i][j];
            data[i][j] = data[i][j+1];
            data[i][j+1] = temp;
         }
    }   
}

还要检查fopen() 的返回值来检查它是否成功 r 失败并进行适当的验证。例如

fp = fopen("data.txt","r");
if(fp == NULL) {
   /* @TODO error handling */
   fprintf(stderr,"file doesn't exist");
   return 0;
}

【讨论】:

    【解决方案2】:

    首先,您似乎应该声明 char data[20][100] 而不是 char data[100][20]

    然后,在 20 次迭代的循环中,您应该引用 data[i] 而不是 data

    for (int i=0; i<20; i++)
        fscanf(fp,"%s",data[i]);
        printf("%s\n",data[i]);
    }
    

    请记住,您假设输入文件中的每一行最长为 99 个字符。

    这并不能回答我认为的实际问题,但您至少应该解决上述所有问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-11
      • 2013-01-05
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 2021-02-01
      相关资源
      最近更新 更多