fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class SymbolTableEntry {
  7. public:
  8. string name;
  9. string type;
  10. string size;
  11. string dimension;
  12. string line_of_code;
  13. string address;
  14.  
  15. SymbolTableEntry(string name, string type, string size, string dimension, string line_of_code, string address)
  16. : name(name), type(type), size(size), dimension(dimension), line_of_code(line_of_code), address(address) {}
  17.  
  18. friend ostream& operator<<(ostream& os, const SymbolTableEntry& entry) {
  19. return os << "Name: " << entry.name << ", Type: " << entry.type << ", Size: " << entry.size
  20. << ", Dimension: " << entry.dimension << ", Line of Code: " << entry.line_of_code
  21. << ", Address: " << entry.address;
  22. }
  23. };
  24.  
  25. class SymbolTable {
  26. private:
  27. static const int TABLE_SIZE = 100;
  28. vector<vector<SymbolTableEntry>> table;
  29.  
  30. int hashFunction(const string& name) const {
  31. int hashValue = 0;
  32. for (char c : name) {
  33. hashValue += static_cast<int>(c);
  34. }
  35. return hashValue % TABLE_SIZE;
  36. }
  37.  
  38. public:
  39. SymbolTable() : table(TABLE_SIZE) {}
  40.  
  41. void insert(const SymbolTableEntry& entry) {
  42. int hashValue = hashFunction(entry.name);
  43. table[hashValue].push_back(entry);
  44. }
  45.  
  46. SymbolTableEntry* search(const string& name) {
  47. int hashValue = hashFunction(name);
  48. vector<SymbolTableEntry>& entries = table[hashValue];
  49. for (SymbolTableEntry& entry : entries) {
  50. if (entry.name == name) {
  51. return &entry;
  52. }
  53. }
  54. return nullptr;
  55. }
  56.  
  57. bool remove(const string& name) {
  58. int hashValue = hashFunction(name);
  59. vector<SymbolTableEntry>& entries = table[hashValue];
  60. for (auto it = entries.begin(); it != entries.end(); ++it) {
  61. if (it->name == name) {
  62. entries.erase(it);
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68.  
  69. bool update(const string& name, const SymbolTableEntry& newEntry) {
  70. int hashValue = hashFunction(name);
  71. vector<SymbolTableEntry>& entries = table[hashValue];
  72. for (SymbolTableEntry& entry : entries) {
  73. if (entry.name == name) {
  74. entry = newEntry;
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80.  
  81. void showContents() const {
  82. for (const vector<SymbolTableEntry>& entries : table) {
  83. for (const SymbolTableEntry& entry : entries) {
  84. cout << entry << endl;
  85. }
  86. }
  87. }
  88.  
  89. int getHashKey(const string& name) const {
  90. return hashFunction(name);
  91. }
  92. };
  93.  
  94. int main() {
  95. SymbolTable symbolTable;
  96.  
  97. SymbolTableEntry entry1("Jamal", "Int", "2", "1", "5", "0x6dfed4");
  98. symbolTable.insert(entry1);
  99.  
  100. SymbolTableEntry* result = symbolTable.search("Jamal");
  101. if (result != nullptr) {
  102. cout << "Entry found:" << endl;
  103. cout << *result << endl;
  104. } else {
  105. cout << "Entry not found." << endl;
  106. }
  107.  
  108. bool deleted = symbolTable.remove("Jamal");
  109. if (deleted) {
  110. cout << "Entry deleted." << endl;
  111. } else {
  112. cout << "Entry not found." << endl;
  113. }
  114.  
  115. symbolTable.insert(entry1);
  116.  
  117. SymbolTableEntry entry2("Kamal", "Char", "3", "1", "6", "0x7ffdd8747");
  118. bool updated = symbolTable.update("Jamal", entry2);
  119. if (updated) {
  120. cout << "Entry updated." << endl;
  121. } else {
  122. cout << "Entry not found." << endl;
  123. }
  124.  
  125. symbolTable.showContents();
  126.  
  127. int hashKey = symbolTable.getHashKey("Kamal");
  128. cout << "Hash key: " << hashKey << endl;
  129.  
  130. return 0;
  131. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Entry found:
Name: Jamal, Type: Int, Size: 2, Dimension: 1, Line of Code: 5, Address: 0x6dfed4
Entry deleted.
Entry updated.
Name: Kamal, Type: Char, Size: 3, Dimension: 1, Line of Code: 6, Address: 0x7ffdd8747
Hash key: 86