原代码如下,一直发现不到问题
上网搜了一下专业说法是:
返回值 3221226356 在 Windows 平台上通常是由于程序发生了访问非法内存的错误,也就是所谓的“段错误”(Segmentation Fault)。
常见的错误可能是malloc错误或者L本身是空指针但是访问L->Next
检查后发现是H->Heads=(List)malloc(sizeof(struct TblNode)*(H->TableSize));错了
应该改成H->Heads=(List)malloc(sizeof(struct VNode)*(H->TableSize));
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define KEYLENGTH 11
typedef char ElementType[KEYLENGTH+1];
typedef struct VNode*PtrToVNode;
typedef int Index;
struct VNode
{
ElementType Data;
PtrToVNode Next;
int cnt;
};
typedef PtrToVNode Position;
typedef PtrToVNode List;
typedef struct TblNode*HashTable;
struct TblNode
{
List Heads;
int TableSize;
};
#define MAX 1000000
int NextPrime(int N)
{
int p=(N%2)?N+2:N+1;
while(p<MAX)
{
int i;
for(i=(int)sqrt(p);i>2;i--)
{
if(!(p%i))
{
break;
}
}
if(i==2)
{
break;
}
else
{
p+=2;
}
}
return p;
}
#define move 5
int Hash(ElementType P,int TableSize)
{
int key=atoi(P+KEYLENGTH-move);
key%=TableSize;
return key;
}
HashTable CreateHashTable(int TableSize)
{
HashTable H=(HashTable)malloc(sizeof(struct TblNode));
H->TableSize=NextPrime(TableSize);
H->Heads=(List)malloc(sizeof(struct TblNode)*(H->TableSize));
for(int i=0;i<H->TableSize;i++)
{
H->Heads[i].Data[0]=0;
H->Heads[i].Next=NULL;
H->Heads[i].cnt=0;
}
return H;
}
Position Find(HashTable H,ElementType Key)
{
Index Pos=Hash(Key,H->TableSize);
Position P=H->Heads[Pos].Next;
while(P&&strcmp(P->Data,Key))
{
P=P->Next;
}
return P;
}
int Insert(HashTable H,ElementType Key)
{
Position P=Find(H,Key);
Index Pos=Hash(Key,H->TableSize);
if(!P)
{
Position newnode=(Position)malloc(sizeof(struct VNode));
newnode->cnt=1;
strcpy(newnode->Data,Key);
newnode->Next=H->Heads[Pos].Next;
H->Heads[Pos].Next=newnode;
return 1;
}
else
{
P->cnt++;
return 0;
}
}
int main()
{
int n;
scanf("%d",&n);
HashTable H=CreateHashTable(2*n);
// printf("%c",H->Heads[0].Data[0]);
for(int i=0;i<2*n;i++)
{
ElementType s;
scanf("%s",s);
Insert(H,s);
}
return 0;
}
修改后代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define KEYLENGTH 11
typedef char ElementType[KEYLENGTH+1];
typedef struct VNode*PtrToVNode;
typedef int Index;
struct VNode
{
ElementType Data;
PtrToVNode Next;
int cnt;
};
typedef PtrToVNode Position;
typedef PtrToVNode List;
typedef struct TblNode*HashTable;
struct TblNode
{
List Heads;
int TableSize;
};
#define MAX 1000000
int NextPrime(int N)
{
int p=(N%2)?N+2:N+1;
while(p<MAX)
{
int i;
for(i=(int)sqrt(p);i>2;i--)
{
if(!(p%i))
{
break;
}
}
if(i==2)
{
break;
}
else
{
p+=2;
}
}
return p;
}
#define move 5
int Hash(ElementType P,int TableSize)
{
int key=atoi(P+KEYLENGTH-move);
key%=TableSize;
return key;
}
HashTable CreateHashTable(int TableSize)
{
HashTable H=(HashTable)malloc(sizeof(struct TblNode));
H->TableSize=NextPrime(TableSize);
H->Heads=(List)malloc(sizeof(struct VNode)*(H->TableSize));
for(int i=0;i<H->TableSize;i++)
{
H->Heads[i].Data[0]=0;
H->Heads[i].Next=NULL;
H->Heads[i].cnt=0;
}
return H;
}
Position Find(HashTable H,ElementType Key)
{
Index Pos=Hash(Key,H->TableSize);
Position P=H->Heads[Pos].Next;
while(P&&strcmp(P->Data,Key))
{
P=P->Next;
}
return P;
}
int Insert(HashTable H,ElementType Key)
{
Position P=Find(H,Key);
Index Pos=Hash(Key,H->TableSize);
if(!P)
{
Position newnode=(Position)malloc(sizeof(struct VNode));
newnode->cnt=1;
strcpy(newnode->Data,Key);
newnode->Next=H->Heads[Pos].Next;
H->Heads[Pos].Next=newnode;
return 1;
}
else
{
P->cnt++;
return 0;
}
}
int main()
{
int n;
scanf("%d",&n);
HashTable H=CreateHashTable(2*n);
// printf("%c",H->Heads[0].Data[0]);
for(int i=0;i<2*n;i++)
{
ElementType s;
scanf("%s",s);
Insert(H,s);
}
return 0;
}