Advertisement
smatskevich

HashTable

Apr 28th, 2015
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <assert.h>
  5. using std::string;
  6. using std::vector;
  7. using std::cin;
  8. using std::cout;
  9.  
  10. const int HashParamA = 37;
  11. const int InitialTableSize = 8;
  12.  
  13. int Hash( const string& s, int m ) {
  14.         int result = 0;
  15.         for( int i = 0; i < static_cast<int>( s.length() ); ++i ) {
  16.                 result = ( result * HashParamA + s[i] ) % m;
  17.         }
  18.         return result;
  19. }
  20.  
  21. class CHashTable {
  22. public:
  23.         CHashTable();
  24.  
  25.         bool Has( const string& s );
  26.         // Возвращает false, если элемент уже есть.
  27.         bool Add( const string& s );
  28.         // Возвращает false, если элемента нет.
  29.         bool Remove( const string& s );
  30.  
  31. private:
  32.         struct CNode {
  33.                 string Data;
  34.                 CNode* Next;
  35.  
  36.                 CNode( const string& data ) : Data( data ), Next( 0 ) {}
  37.         };
  38.         vector<CNode*> table;
  39.         int size; // Количество элементов.
  40. };
  41.  
  42. CHashTable::CHashTable() :
  43.         size( 0 )
  44. {
  45.         table.resize( InitialTableSize, 0 );
  46. }
  47.  
  48. bool CHashTable::Has( const string& s ) {
  49.         int hash = Hash( s, table.size() );
  50.         for( CNode* node = table[hash]; node != 0; node = node->Next ) {
  51.                 if( node->Data == s ) {
  52.                         return true;
  53.                 }
  54.         }
  55.         return false;
  56. }
  57.  
  58. bool CHashTable::Add( const string& s ) {
  59.         int hash = Hash( s, table.size() );
  60.         if( table[hash] == 0 ) {
  61.                 table[hash] = new CNode( s );
  62.         ++size;
  63.                 return true;
  64.         }
  65.  
  66.         CNode* currentNode = table[hash];
  67.         while( true ) {
  68.                 if( currentNode->Data == s ) {
  69.                         return false;
  70.                 }
  71.                 if( currentNode->Next == 0 ) {
  72.                         break;
  73.                 }
  74.                 currentNode = currentNode->Next;
  75.         }
  76.         currentNode->Next = new CNode( s );
  77.     ++size;
  78.         return true;
  79. }
  80.  
  81. bool CHashTable::Remove( const string& s ) {
  82.         int hash = Hash( s, table.size() );
  83.         if( table[hash] == 0 ) {
  84.                 return false;
  85.         }
  86.         if( table[hash]->Data == s ) {
  87.                 CNode* toDelete = table[hash];
  88.                 table[hash] = table[hash]->Next;
  89.                 delete toDelete;
  90.         --size;
  91.                 return true;
  92.         }
  93.         CNode* node = table[hash];
  94.         while( node->Next != 0 ) {
  95.                 if( node->Next->Data == s ) {
  96.                         CNode* toDelete = node->Next;
  97.                         node->Next = toDelete->Next;
  98.                         delete toDelete;
  99.             --size;
  100.                         return true;
  101.                 }
  102.         }
  103.         return false;
  104. }
  105.  
  106. int main() {
  107.         CHashTable hashTable;
  108.         char command = 0;
  109.         string data;
  110.         while( cin >> command >> data ) {
  111.                 switch( command ) {
  112.                         case '+':
  113.                                 cout << ( hashTable.Add( data ) ? "OK" : "FAIL" ) << std::endl;
  114.                                 break;
  115.                         case '-':
  116.                                 cout << ( hashTable.Remove( data ) ? "OK" : "FAIL" ) << std::endl;
  117.                                 break;
  118.                         case '?':
  119.                                 cout << ( hashTable.Has( data ) ? "OK" : "FAIL" ) << std::endl;
  120.                                 break;
  121.                         default:
  122.                                 assert( false );
  123.                 }
  124.         }
  125.         return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement