CodePudding user response:
What do you think this is the average person to write?Go to the standard library or baidu, don't have time to write to you so long code,
And only 20 points
Give a hint:
Template
The class hash_list {
//...
};
CodePudding user response:
//the design and implementation of a hash table class template
The template & lt; The class T, class N>
Struct HashTableNode
{
T varValue;
N varName;
HashTableNode* next;
N the Scope;
N alias.//will hold the full name of whatever it is pointing to.
};
The template & lt; The class T, class N>
The class hashtable
{
Private:
N currentScope ()
{
Value (); return the Scope.
}
Int hash (N varName, int moder=10)
{
int i=0;
For (int q=0; (q & lt; VarName. Len () + 1); Q++)
I=I + (q * varName [q]);
I=I % moder;
return i;
}
Public:
HashTableNode* HTNode [10].
Hashtable ()
{
For (int I=0; I & lt; 10; I++)
HTNode [I]=NULL;
Global Scope. Push (" ");
ScopeID="GlobalScope";
}
~ hashtable ()
{
for (int i=0; I<10; I++)
{
HTNode [I]=NULL;
}
}
Bool newScope ScopeName (N="")
{
If (ScopeID=="")
{
ScopeID="SystemScope";
//return true;
}
If (ScopeName!="")
{
node* temp;
Temp=Scope. The First;
while (temp !=NULL)
{
If (temp - & gt; Value=ScopeName https://bbs.csdn.net/topics/=
{
NewScope (ScopeName + "X");
return true;
}
Temp=temp - & gt; Next;
}
The delete [] temp;
Scope. Push (ScopeName);
If (Debug) cout & lt; <"Starting" & lt;return true;
}
The else
{
ScopeID +="X";
Scope. Push (ScopeID);
If (Debug) cout & lt; <"Starting" & lt;return true;
}
}
Bool endScope ()
{
N temp;
Temp=currentScope ();
HashTableNode* t;
for (int i=0; I & lt; 10; I + +)
{
T=HTNode [I];
If (t!=NULL)
{
While ((t!=NULL) & amp; & (t - & gt; The Scope==temp))
{
HTNode=[I] t - & gt; Next;
The delete t;
T=HTNode [I];
}
}
}
Temp=Scope. Pop ();
If (Debug) cout & lt; <"Ending" & lt;If (Scope. The Value ()!="") to return true;
return false;
}
VarValue varName bool invoke the (N, T, N iScope="")
{
int i=0;
N S;
S=currentScope ();
If (iScope! S=="") iScope;
I=the hash (varName);
HashTableNode* t;
T=HTNode [I];
While (t!=NULL)
{
If ((t - & gt; VarName==varName) & amp; & (t - & gt; The Scope==S)) return false;
T=t - & gt; Next;
}
T=new HashTableNode;
T - & gt; VarValue=https://bbs.csdn.net/topics/varValue;
T - & gt; VarName=varName;
T - & gt; The Scope=S;
T - & gt; Next=HTNode [I];
T - & gt; Alias="";
HTNode [I]=t;
return true;
}
AliasName varName bool addAlias (N, N, N iScope="")
{//varName is the pointer AliasName is the where it points
int i=0;
N S;
S=currentScope ();
If (iScope! S=="") iScope;
I=the hash (varName);
HashTableNode* t;
T=HTNode [I];
While (t!=NULL)
{
If ((t - & gt; VarName==varName) & amp; & (t - & gt; The Scope==S)) return false;
T=t - & gt; Next;
}
T=new HashTableNode;
T - & gt; VarName=varName;
T - & gt; The Scope=S;
T - & gt; Next=HTNode [I];
T - & gt; Alias=AliasName;
HTNode [I]=t;
return true;
}
Bool ChangeNode (N varName, T varValue)
{
N S (" ");
If (varName. InStr (' : ') & gt; 0)
{
S=Tokenize (varName, ":");
VarName. LTrim (', ');
}
int i=0;
I=the hash (varName);
HashTableNode* t;
T=HTNode [I];
If (S!="")
While (t!=NULL)
{
If ((t - & gt; VarName==varName) & amp; & (t - & gt; The Scope==S))
{
If (t - & gt; Alias=="")
T - & gt; VarValue=https://bbs.csdn.net/topics/varValue;
The else
ChangeNode (t - & gt; Alias, varValue);
return true;
}
T=t - & gt; Next;
}
The else
While (t!=NULL)
{
If (t - & gt; VarName==varName)
{
If (t - & gt; Alias=="")
T - & gt; VarValue=https://bbs.csdn.net/topics/varValue;
The else
ChangeNode (t - & gt; Alias, varValue);
return true;
}
T=t - & gt; Next;
}
return false;
}
T Value (N varName)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull