C++ C++;
我有一个程序,它声称在C语言中实现了一个哈希函数。这个程序编译得很好,但是当我输入元素时,程序崩溃了。这是节目单C++ C++;,c++,hash,C++,Hash,我有一个程序,它声称在C语言中实现了一个哈希函数。这个程序编译得很好,但是当我输入元素时,程序崩溃了。这是节目单 #include <iostream> #include <windows.h> #define HASHMAX 10 using namespace std; typedef struct nodeL { int info; nodeL * nxt; nodeL * pre; } node; typedef node* _lis
#include <iostream>
#include <windows.h>
#define HASHMAX 10
using namespace std;
typedef struct nodeL
{
int info;
nodeL * nxt;
nodeL * pre;
} node;
typedef node* _list;
HashInsert过程,其中数字插入到哈希中
void HashInsert (Hash &h, int e)
{
enlist ((h)[HashKey(e)],e);
}
void enlist (_list &L, int n)
{
_list aux = new nodeL;
if(L==NULL)
{
aux -> info = n;
aux -> nxt = L;
L=aux;
}
else
{
if(L->info>n)
{
aux -> info = n;
aux -> pre = L;
L=aux;
}
else
{
enlist(L->nxt,n);
}
}
}
展示程序
int show (_list L)
{
int i=0;
if(L!=NULL)
{
i=1;
cout << "[" << L->info << "]->" << endl;
show(L->nxt);
}
return i;
}
void HashShow(Hash h)
{
int i, n;
for (n=0; n<HASHMAX; n++)
{
cout << "Hash [" << n << "]:";
i=show(h[n]);
if(i==0)cout << "Empty list" << endl << endl;
else cout << "# " << endl << endl;
}
}
void delete_element(_list &L, int n)
{
_list aux = L;
_list pre = NULL;
if(aux==NULL)
{
cout << "Error: Empty list" << endl << endl;
}
else
{
while (aux->info!=n&&aux->nxt!=NULL)
{
pre=aux;
aux=aux->nxt;
}
if(aux->nxt==NULL&&aux->info!=n)
cout << "Error, number is not in the list" << endl;
else
{
pre->nxt=aux->nxt;
delete aux;
}
}
}
int show(_listl)
{
int i=0;
如果(L!=NULL)
{
i=1;
如果输入/输出处理与您的问题不相关,请将其取出,对使其失效的示例输入进行硬编码。制作一个代码块,该代码块仅包含足够的代码(最小)以复制(可复制)您的问题。要解决经典问题:使用命名空间std
被认为是不好的做法,您应该避免这种做法。而且并非所有人都在windows上,如果您不依赖windows.h
,您将增加您的受众。(如果您不想执行任何操作,让我们跳到扰流板:Hash h;
在main
中-您未能初始化此项,这本身就足以导致崩溃)
void delete_element(_list &L, int n)
{
_list aux = L;
_list pre = NULL;
if(aux==NULL)
{
cout << "Error: Empty list" << endl << endl;
}
else
{
while (aux->info!=n&&aux->nxt!=NULL)
{
pre=aux;
aux=aux->nxt;
}
if(aux->nxt==NULL&&aux->info!=n)
cout << "Error, number is not in the list" << endl;
else
{
pre->nxt=aux->nxt;
delete aux;
}
}
}
int main()
{
int op=-1;
int i;
_list mylist=NULL;
Hash h;
while(op)
{
system("cls");
cout << "Hash Example" << endl << endl << "\tSelect an option"<< endl << endl <<"\t-1. Add item to front"<< endl << "\t-2. Show list" << endl << "\t-0. Exit" << endl;
cin >> op;
switch(op)
{
case 1:
{
int e, n;
system("cls");
cout << "Enter the hash key for the item:" << endl;
cin >> n;
cout << "Enter the number to add to the list" << endl;
cin >> e;
HashInsert(h,e);
cout << e << " has been successfully added to the list" << endl;
system("pause");
break;
}
case 2:
{
system("cls");
printf("The numbers loaded in the list are:\n\n");
i = show(mylist);
if(i==0)
cout << "The list is empty" << endl << endl;
else
cout << "# " << endl << endl;
system("pause");
}
break;
}
}
system("pause");
return 0;
}