Home > Software engineering >  Hash table application problem?
Hash table application problem?

Time:09-24

A problem the hash table program cannot achieve the function of hash table delete there will be a random value output
Great god help me have a look at how change is genuflect is begged
Here is the source code:
 # include 
# include
# define SUCCESS 1;
# define UNSUCCESS 0;
# define NULLKEY - 1;
# define Tablelength 13;
# define p 13;//H=key (key) % p
# define MAXSIZE 25

Typedef int T;
Template
Struct ElemType
{
T the key;//keyword
};
The template & lt; The class T>
The class LHSearch
{
Private:
ElemType * HT.//open addressing hash table
int count;//the number of data elements
int size;//length of hash table
Public:
LHSearch ();//
~ LHSearch ();//
Void InitHashTable (int n);//
Int the Hash key (T);//calculate the hash address
Void Collision (int & amp; S);//conflict, computing the next address
Int Search (T key, int & amp; S);//hash lookup
Int the Insert (ElemType E);//element insert
Int the Delete (ElemType E);//delete
elementVoid the Display ();//show the hash table
};
Template
LHSearch : : LHSearch ()
{
HT=NULL;
Size=0;
Count=0;
}
Template
LHSearch : : ~ LHSearch ()
{
The delete [] HT.
Count=0;
}
Template
Int LHSearch Key: : Hash (T)
{//by the hash function for hash address
The return key % p;
}
Template
Void LHSearch : : a Collision (int & amp; S)
{//open addressing conflict resolution
S=s++;
}
The template & lt; The class T>
Int LHSearch : : Search (T key, int & amp; S)
{//search, find back
//int s;
S=Hash (key);
While ((HT [s]. The key!=1) & amp; & (the key!=HT [s] key))
A Collision (s);
If (HT [s]. Key==key)
return 1;
The else
return 0;
}
The template & lt; The class T>
Int LHSearch : : Insert (ElemType E)
{//insert element
Int s;
If (count==size)
{
Printf (" table full, can't insert! \n");
Return UNSUCCESS.
}
The else
{
S=Hash (e.k ey);
Int f;
F=Search (e.k ey, s);
If (f)//in the table and existing e key words of the same element, do not insert
{
Printf (" the element already exist, can't insert! \n");
Return UNSUCCESS.
}
The else
{
HT [s]. Key=e.k ey.
Printf (" insert success! \n");
count++;
The return SUCCESS.
}
}

}

The template & lt; The class T>
Int LHSearch : : Delete (ElemType E)
{
//delete elements
The int "s, I;
If (count==NULL)
{
Printf (" table is empty, cannot be deleted! \n");
Return UNSUCCESS.
}
The else
{
S=Hash (e.k ey);
Int q;
Q=Search (e.k ey, s);
If (q)
{
While (HT [s] key)
HT [s++] key=HT [s + 1] key.
Printf (" deleted successfully! \n");
count--;
The return SUCCESS.
}
The else {
Printf (" this element does not exist, cannot be deleted! \n");
Return UNSUCCESS.
}
}
}
Template
Void LHSearch : : InitHashTable (int n)
{
Size=n;
HT=new ElemType [size];
for(int i=0; iHT [I] key=NULLKEY;
}
Template
Void LHSearch : : the Display ()
{
int i;
for(i=0; iIf (HT [I]. The key!=1)
Printf (" % d \ t ", I);
printf("\n");
for(i=0; iIf (HT [I]. The key!=1)
Printf (" % d \ t ", HT [I] key);
printf("\n");
}
Void main ()
{
int m;
T the key;
Int s=0;
ElemType e;
LHSearch a;
Printf (" input the corresponding code, you must first create a hash table \ n ");
Printf (" \ t | | \ t \ n ");
Printf (" \ t | | hash table application \ t \ n ");
Printf (" \ t | welcome to use! | \ t \ n ");
Printf (" \ t |================================================| \ t \ n ");
Printf (" \ t | | \ t \ n ");
Printf (" \ t | [1] - & gt; Set up! \ t [2] - & gt; Insert! \ t [3] - & gt; Deleted! | \ t \ n ");
Printf (" \ t | | \ t \ n ");
Printf (" \ t | [4] - & gt; Looking for! \ t [5] - & gt; Quit! | \ t \ n ");
Printf (" \ t | | \ t \ n ");
Do
{
Printf (" please select you want to operate: ");
The scanf (" % d ", & amp; M);
The switch (m)
{
Case 1://create a lookup table
//printf (" please enter the table size: \ n ");
//the scanf (" % d ", & amp; M);
Anderson nitHashTable (MAXSIZE);
Printf (" the end of the table element, please input: \ n ");
For (the scanf (" % d ", & amp; E.k ey); E.k ey!=1; The scanf (" % d ", & amp; E.k ey))
Anderson, nsert (e);
A. d. isplay ();
break;
Case 2://insert element
Printf (" input in order to insert elements: \ n ");
The scanf (" % d ", & amp; E.k ey);
Anderson, nsert (e);
A. d. isplay ();
break;
Case 3://delete elements
Printf (" input to delete elements: \ n ");
The scanf (" % d ", & amp; E.k ey);
A. d. elete (e);
A. d. isplay ();
break;
Case 4://look for the element
Printf (" input search key words: \ n ");
The scanf (" % d ", & amp; Key);
If (a.S earch (key, s))
Printf (" found! \n");
The else
Printf (" does not exist, not found! \n");
break;
Case 6://end
Printf (" the end! Thank you for your use of \ n ");
break;
}
}
While (m!=5);
}

CodePudding user response:

Hash table can't be too full, otherwise the conflict!

CodePudding user response:

Debug the program must be one of the skillsnullnullnullnullnullnullnullnullnullnullnull
  • Related