我正在尝试将数组用作哈希表,每个数组都指向自己的链表
size是检查链表的节点数是否为32。
我的问题是我得到分段错误,但我的指针中看不到任何错误,这是完整的代码。
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
typedef int bool;
enum { false, true };
void main(int argc, char *argv[])
{
// create linked list---------------------------
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *first, *last, *temp, *newNode=0 ;
int count = 0;
first = NULL;
last=NULL;
temp=NULL;
newNode=0;
//-----------------------------------------------------------------------
//filling the text file with billions of integers------------------------------------------------
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
int i = 0;
unsigned long long randomvalue;
for (i = 0; i < 10000000; i++)
{
randomvalue = random();
randomvalue <<= 16; // just picked 16 at random
randomvalue ^= random(); // you could also use + but not "or";
randomvalue %= 10000000000ULL;
fprintf(f,"%lld \n",randomvalue);
}
fclose(f);
NODE* array[312500];
first = (NODE *)malloc(sizeof(NODE));
last= (NODE *)malloc(sizeof(NODE));
temp = (NODE *)malloc(sizeof(NODE));
newNode = (NODE *)malloc(sizeof(NODE));
FILE *file = fopen ("file.txt", "r");
int x=0;
for ( x=0; x<=312500; x++)
{
while (count <=32)
{
fscanf (file, "%d", &temp->num);
temp->ptr=NULL;
newNode->num=temp->num;
newNode->ptr=NULL;
if (first != 0)
{
last->ptr=newNode;
last=newNode;
count=count+1;
}
else
{
first = newNode;
last = newNode;
count=count+1;
}
fflush(stdin);
newNode->ptr=0;
newNode=NULL;
}
count =0;
array[x]->ptr=first;
first->ptr=0;
first=NULL;
last->ptr=0;
last=NULL;
}
fclose (file);
temp->ptr = 0;
temp=NULL;
}