hashtable在C中给出错误的输出

时间:2016-10-27 09:09:08

标签: java c hashtable

我已经创建了哈希表来解决hackerearth使用java的问题之一。 链接:https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/mind-palaces-3/ 我的java解决方案能够通过所有测试用例。 现在有了相同的逻辑,我在C中创建解决方案。 但是通过C中的解决方案并没有通过所有测试用例。 我正在学习C. 请帮我在C代码中找到问题。 谢谢。 我的Java代码:

package hashtable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MindPalces {
    public static void main(String[] args) throws NumberFormatException,
            IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String string = br.readLine();
        String strArr[] = string.split(" ");
        int n = Integer.parseInt(strArr[0]);
        int m = Integer.parseInt(strArr[1]);
        SinglyLinkLists hashTable[] = new SinglyLinkLists[337];

        long array[][] = new long[n][m];

        for (int i = 0; i < n; i++) {
            String string1 = br.readLine();
            String[] innerArr = string1.split(" ");
            for (int j = 0; j < m; j++) {
                array[i][j] = Long.parseLong((innerArr[j]));
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                long data = array[i][j];
                if (data < 0) {
                    data = data * -1;
                }
                int index = Hashs.hashFunction(data);

                if (hashTable[index] == null) {
                    SinglyLinkLists linkList = new SinglyLinkLists();
                    linkList.insertAtEnd(i, j, array[i][j]);
                    hashTable[index] = linkList;

                } else {
                    SinglyLinkLists linkList = hashTable[index];
                    linkList.insertAtEnd(i, j, array[i][j]);
                }
            }
        }

        int q = Integer.parseInt(br.readLine());
        for (int i = 0; i < q; i++) {
            long val = Long.parseLong(br.readLine());
            long val1 = val;
            if (val < 0) {
                val = val * -1;
            }
            int index = Hashs.hashFunction(val);

            if (hashTable[index] == null) {
                System.out.println("-1" + " " + "-1");

            } else {
                SinglyLinkLists linkList = hashTable[index];
                NOde list = linkList.getNode(val1);
                if (null != list) {
                    System.out.println(list.getI1() + " " + list.getJ1());
                } else {
                    System.out.println("-1" + " " + "-1");
                }

            }
        }
    }
}

class Hashs {
    public static int hashFunction(long key) {

        return (int) (key % 337);
    }
}

class NOde {
    int i1 = 0;
    int j1 = 0;
    NOde link = null;
    long number = 0;

    public NOde() {

        link = null;
        i1 = 0;
        j1 = 0;
        number = 0;
    }

    public NOde(NOde node, int i1, int j1, long number) {

        this.i1 = i1;
        this.j1 = j1;
        link = node;
        this.number = number;
    }

    public NOde getLink() {
        return link;
    }

    public void setLink(NOde link) {
        this.link = link;
    }

    public int getI1() {
        return i1;
    }

    public void setI1(int i1) {
        this.i1 = i1;
    }

    public int getJ1() {
        return j1;
    }

    public void setJ1(int j1) {
        this.j1 = j1;
    }

    public long getNumber() {
        return number;
    }

    public void setNumber(long number) {
        this.number = number;
    }
}

class SinglyLinkLists {

    NOde start = null;
    NOde end = null;
    int size = 0;

    public SinglyLinkLists() {
        start = null;
        end = null;
        size = 0;

    }

    public void insertAtEnd(int i1, int j1, long number) {
        NOde nptr = new NOde(null, i1, j1, number);
        size++;
        if (start == null) {

            start = nptr;
            end = start;

        } else {

            end.setLink(nptr);
            end = nptr;
        }
    }

    // Function to display elements
    public NOde getNode(long val) {

        if (start.getNumber() == val) {

            return start;
        }
        NOde ptr = start;
        ptr = start.getLink();
        while (null != ptr && ptr.getLink() != null) {
            if (ptr.getNumber() == val) {
                return ptr;
            }
            ptr = ptr.getLink();
        }
        if (null != ptr && ptr.getNumber() == val) {
            return ptr;
        }
        return null;
    }
}

我的C代码:

#include<stdio.h>
#include <stdlib.h>
struct Node
{
    int i1;
    int j1;
    long inputVal;
    struct Node * next;
};

struct hash
{

    struct Node * head;
};
struct hash *hashTable=NULL;


struct Node *createNode(int i,int j,long number)
{

    struct Node *list;
    list = (struct Node *) malloc(sizeof(struct Node));
    list->i1 = i;
    list->j1 = j;
    list->inputVal=number;
    list->next = NULL;

    return list;
};

int main()
{
    int m=0,n=0;
    scanf("%d%d",&m,&n);

    hashTable=(struct hash *)calloc(337,sizeof(struct Node));

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            long data=0;
            long copy=0;

            scanf("%ld",&data);
            copy=data;
            if(data<0)
            {

                data=data*-1;
            }
            int index=hashFunction(data);

            struct Node *newnode = createNode(i,j,copy);
            struct Node *myNode = hashTable[index].head;

            if (!myNode)
            {
                hashTable[index].head=newnode;
            }
            else
            {
                newnode->next=hashTable[index].head;
                hashTable[index].head=newnode;

            }
        }
    }

    int q=0;
    scanf("%d",&q);
    for(int i=0; i<q; i++)
    {
        long val=0;
        long copy=0;
        int boolean=0;
        scanf("%ld",&val);
        copy=val;
        if(val<0)
        {
            val=val*-1;
        }
        int index=hashFunction(val);
        struct Node *myNode = hashTable[index].head;
        if(!myNode)
        {
            printf("%d %d\n",-1,-1);
        }
        else
        {


            while (myNode->next!= NULL)
            {
                if (myNode->inputVal==copy)
                {
                    boolean=1;
                    printf("%d %d\n",myNode->i1,myNode->j1);
                    break;
                }
                myNode = myNode->next;
            }

            if(myNode->inputVal==copy &&!boolean)
            {
                boolean=1;
                printf("%d %d\n",myNode->i1,myNode->j1);

            }
            if(!boolean)
            {
                printf("%d %d\n",-1,-1);
            }
        }
    }

    return 0;
}
int hashFunction(long data)
{
    return (int)(data%337);
}

示例输入:

5 5
-993655555 -758584352 -725954642 -696391700 -649643547
-591473088 -568010221 -432112275 -421496588 -351507172
-323741602 -232192004 -30134637 -369573 100246476
156824549 174266331 392354039 601294716 763826005
768378344 802829330 818988557 992012759 999272829
10
156824549
-758584352
-993655555
601294716
-696391700
802829330
-993655555
-232192004
392354039
-568010221

0 个答案:

没有答案