ERROR:Process exited after 2.687 seconds with return value 3221226356

 原代码如下,一直发现不到问题

上网搜了一下专业说法是:

返回值 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; 
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值